Data Types Obj-C [duplicate] - objective-c

This question already has an answer here:
Closed 11 years ago.
Exact Duplicate:
Issue with float and double data types in objective C
[Ironically, to find the duplicate questions you need to know the answer.]

What Every Computer Scientist Should Know About Floating-Point Arithmetic
If it cannot be expressed in base 2, it will not be precise. See also floating point inaccuracy.

0.1 is a 'repeating decimal' in binary (0.0001100110011...) so the representation of 0.1 is inexact. NSLog is likely rounding or truncating the output.

Related

Is there a Objective-C or C math function to constrain a variable between a Min and a Max? [duplicate]

This question already has answers here:
Fastest way to clamp a real (fixed/floating point) value?
(14 answers)
Closed 6 years ago.
What I am looking for is a math function that constrains a primitive variable between a minimum and a maximum value in only one single function call, if it exists, in the standard math libraries for Objective-C.
I currently use:
float constrainedValue = fminf( maxValue, fmaxf( minValue, inValue ) );
Since I know that both fminf and fmaxf could potentially have instruction jumps or branches, it seems likely there could be a simple routine that could conjoin both of these operations into one, optimized function.
This topic is thoroughly discussed here: Fastest way to clamp a real (fixed/floating point) value?
'clamp' is the keyword I was looking for.

Finding VERY precise percentiles based on Z-Scores in C/Objective-C? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am creating a program that needs to find thousands of individual percentiles, most of which are less than .00005. Currently, to do this I use
0.5 * erfc(-zScore * M_SQRT1_2)
However, this seems to be rounding slightly (throughout the rest of my program I am using doubles and long doubles, so it has to be this). I believe this because at the end, when I add up all the percentiles, I get 1.835468. This tells me that it is rounding as it should add up 1, or at least a number very close to 1. In addition, when I log each individual percentile, I get the same number (let's say 0.000036) for a few percentiles, and then it goes down to 0.000035. It should be going down each time as each number is further from the mean than the last.
I need a way to find very precise percentiles based on Z-Scores, which this is not giving me as it is rounding too early, at the 6th decimal place.
When you see it jump from 0.000036 to 0.000035, this is because you need to use NSLog(#"Value is: %0.36f", yourPercentile);. You should find that it is not actually rounding at 6 digits, but that was just how it was logged.
Now, your error is coming from the fact that you are using doubles, which do not store precise values very well. First, you need to know how much precision that you need, and then use a type which can handle that level of precision.
Let's say that you decide that you require 12 digits of precision.
long long is then a good unit to use because it can store 19 digits. When you calculate your original value, you need to multiply the original values by 100,000,000,000 and store them as long long's. Then do the math that you need using the large values. Eventually, when you get your result, just divide by the same value (or 2 digits less less if you want to see it as a whole number) to get your percentage.
I believe it's a matter of formatting the number: if two values(long double) are very close each other but still different, you will see them equal, specify the format with something like "%.12Lf".

MATLAB dealing with approximation-- singles to doubles

I am pulling financial data into Matlab from SQL, where it is unfortunately stored as a 'Real' (which is an approximate data-type).
For example, a value got loaded into SQL as "96.194" which is the correct value (this could have any number of decimals 1-5). I know in SQL it is stored as something like 96.19400024 because it is an approximation, but SQL Server somehow knows to display it as 96.194.
When I pull it into matlab, it gets pulled in as 96.194, which is what I want. Unfortunately, it turns out it's not actually 96.194, as demonstrated:
>>price
price =
96.194
>> price==96.194
ans =
0
>> class(price)
ans =
single
>> double(price)
ans =
96.1940002441406
So my question is, is there a way to convert a single to a double exactly as it appears as a single (i.e. truncate all the decimals which are the approximation? Note: I cannot just round it because I don't know how many decimals it's supposed to have.
The vpa function lets you specify a number of significant (nonzero) digits that is different from the current digits setting. For example:
vpa(price, num_of_digits_required)
or in your case:
vpa(double(price),7)
(6 or 8 significant digits will yield the same result)
Edit
To use vpa you'll need the Symbolic Math Toolbox, there are alternatives found on the web, such as this FEX file.
Single precision floating point values have only about 7 digits of precision (23 bit fractional component, log10(2^24) ≈ 7.225 decimal digits) so you could round off all but the 7 most significant digits.

Storing and computing with real numbers up to an arbitrary precision in vb.net [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.NET Framework Library for arbitrary digit precision
How can I store a real number, eg, root 2 or one third, up to an arbitrary precision (the precision I need is infinate precision) in vb.net?
I would like to be able to store real numbers and perform operations on them (ie root 2 times root 2) without losing any accuracy - IE storing 1/3 would return the value 1/3 if I needed to retrieve this value.
I was thinking of using a fractal encoding but I am unsure as to the best way to do this.
Storage capacity is not an issue, I just need the real numbers to be 100% accurate.
Will that be a single real number there or does it need to be an arbitrary number of (almost) arbitrary figures? (Sorry for "answer" - for some reason i can't add comments now...)

Accuracy of double Objective-C [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why can't decimal numbers be represented exactly in binary?
When I enter 0.1 as a double value the compiler is adding a tiny value on the end of it that is causing other calculations to go wrong in the program that I am running. My code simply says:
double temp = 0.1;
And I get this in variable viewer:
http://img.skitch.com/20111122-nnrcgi4dtteg8aa3e8926r3fd4.jpg
Does anyone know why this is happening?
Thanks
double is a floating binary point type. In binary, the value of "a half" is 0.1, and the value of "a quarter" is 0.01 etc. There is no way of exactly representing "a tenth" in a finite binary representation, any more than you can exactly represent "a third" in decimal. The compiler is giving you the closest value it can to the value you've actually asked for.
If you want to store decimal values precisely because you care about the decimals (e.g. for current) you should use a decimal-based type such as NSDecimalNumber, or an integer scaled appropriately (e.g. storing 15 for 15 cents instead of 0.15 dollars).
I have articles on binary and decimal floating point in .NET - NSDecimalNumber in Objective-C is slightly different to decimal in C# (see the documentation), but hopefully those articles will give you a bit more insight into what's actually happening.
EDIT: As noted in comments, typically decimal floating point types are significantly slower than binary floating point types, partly because they're often larger and partly because they don't have CPU support. If you have a hard performance requirement and you want to retain digits precisely, the "integer and implied scale" option is usually a good one, though a pain to code against as you need to take it into account every time you read the code :)