MAXFLOAT in Objective-C - objective-c

Max float is defied as:
math.h
#define MAXFLOAT 0x1.fffffep+127f
I'm a little sad I never noticed this before. What's this actually say? I would have expected something like this:
#define MAXFLOAT 0xFFFFFFFF-1
Would that even work?

0x1.fffffep+127 is (roughly)
1.99999999999999999999998 times 2^127. It's a floating point number, with an exponent, in hexadecimal.
0x = hex notation
1 = integer part of the number
.fffffe = fractional part of the number
p+127 = scientific notation for "times two to the 127th power"

MAXFLOAT is required for UNIX conformance:
MAXFLOAT
[XSI] Value of maximum non-infinite single-precision floating-point number.
0x1.fffffep+127f is precisely that value, represented as a standard C hexadecimal floating-point literal.
The C standard requires that FLT_MAX be defined in <float.h>, and it has the same value ("maximum representable finite floating-point number", per §5.2.4.2.2). FLT_MAX is the more portable choice, as it is required by the language standard.

Related

How to write negative number in obj C?

I´m trying to write a negative number like this:
} else if ([newsCondition.temperature floatValue] == -7.0f) {
but that won´t trigger it and the negative symbol is black whilst the number is blue. How can I write the number so that it triggers when temperature isEqual to -7.0 degrees?
The way you've written your negative number (-7.0f) is correct.
As for your code not triggering: the floating point representation of numbers is not perfect, and you have to be aware of these issues when comparing floating point numbers to each other.
If you're wanting to compare two floating point numbers, you can use an 'epsilon' (i.e. acceptable error) for the comparison. This is basically checking if the numbers are close enough.
Simple naive example:
#define EPSILON 0.00001f
float x = 0.09f;
float y = 0.0901f;
if (abs(y - x) < EPSILON) {
// close enough to be considered equal;
// do something here
}
For more discussion, see http://floating-point-gui.de/errors/comparison/
Floating-point arithmetic is considered an esoteric subject by many
people. This is rather surprising because floating-point is ubiquitous
in computer systems. Almost every language has a floating-point
datatype; computers from PCs to supercomputers have floating-point
accelerators; most compilers will be called upon to compile
floating-point algorithms from time to time; and virtually every
operating system must respond to floating-point exceptions such as
overflow. This paper presents a tutorial on those aspects of
floating-point that have a direct impact on designers of computer
systems. It begins with background on floating-point representation
and rounding error, continues with a discussion of the IEEE
floating-point standard, and concludes with numerous examples of how
computer builders can better support floating-point.
From What Every Computer Scientist Should Know About Floating-Point Arithmetic

Convert.ToSingle() from double in vb.net returns wrong value

Here is my question :
If we have the following value
0.59144706948010461
and we try to convert it to Single we receive the next value:
0.591447055
As you can see this is not that we should receive. Could you please explain how does this value get created and how can I avoid this situation?
Thank you!
As you can see this is not that we should receive.
Why not? I strongly suspect that's the closest Single value to the Double you've given.
From the documentation for Single, having fixed the typo:
All floating-point numbers have a limited number of significant digits, which also determines how accurately a floating-point value approximates a real number. A Single value has up to 7 decimal digits of precision, although a maximum of 9 digits is maintained internally.
Your Double value is 0.5914471 when limited to 7 significant digits - and so is the Single value you're getting. Your original Double value isn't exactly 0.59144706948010461 either... the exact values of the Double and Single values are:
Double: 0.5914470694801046146693579430575482547283172607421875
Single: 0.591447055339813232421875
It's important that you understand a bit about how binary floating point works - see my articles on binary floating point and decimal floating point for more background.
When converting from double to float you're also rounding. The result should be the single-precision number that is closest to the number you are rounding.
That is exactly what you're getting here.
Floating-point numbers between 0.5 and 1 are of the form n / 2^24 where n is between 2^23 and 2^24.
0.59144706948010461... = 9922835.23723472274456576... / 2^24
so the closest single-precision floating-point number is
9922835 / 2^24 = 0.5914470553...

uint8_t to two's complement function

I'm using objective-c in xcode. How can I convert a uint8_t piece of data into a decimal two's complement? The range is -127 to 127, correct?
If I have:
uint8_t test = 0xF2
Is there a function or method built in that I can use? Does someone have a simple function?
Thanks!
Does this do what you want?
int8_t twosComplement = (int8_t)test;
The question seems a bit confused. It asks to convert to decimal 2's complement, but 2's complement is meaningful only in binary, not in decimal.
If you want to make a unit9_t value into a signed value, you can
- cast it to some signed type like so: (int16_t)unsigned8variable
- assign it to a variable that has a signed type
However, beware of overflow. Your uint8_t value can be anything from 0 to 255. If you assign to an 8-bit signed type, there are representations for values from -128 to +127, and any original value greater than 127 will suddenly appear to be negative. Choose a type that's big enough to hold any value you might actually see. int16_t would be safe because it goes up to 32767.

What does the floating point "f" designator signify?

I wonder if someone can clarify what the "f" behind a floating point number is used to signify?
float myFloat = 12.0f;
as apposed to:
float myFloat = 12.0;
I have seen this used many times but its pretty hard to find an explanation either online or in books. I am assuming its either something carried over from another language thats supported for consistency by C or maybe its there as a directive for the compiler when it comes to evaluate the maths.
I am just curious if there is any practical difference between the "f" and using the "." to signify a floating point number?
It means it's a single-precision float rather than a double precision double.
From the C99 standard:
An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float.
Objective-C is based on C, maybe not C99, but this convention has been around in C for a long time.
There are sometimes performance concerns when converting from float to double, and you can avoid them by using the 'f'. Also when doing say a square root, sin,cos, etc, a wild guess would say that
float answer = sqrt(12.0f)
is about 10x slower than
float answer = sqrtf(12.0f)
It really makes a difference on the phone and iPad, if you are doing millions of these kinds of operations. Stay in float if you need speed and can deal with the lower resolution. If you are not good at math, or not using much math in your program use double everywhere, as there are more gotchas when using the lower precision 32 bit float.

Trouble with floats in Objective-C

I've a small problem and I can't find a solution!
My code is (this is only a sample code, but my original code do something like this):
float x = [#"2.45" floatValue];
for(int i=0; i<100; i++)
x += 0.22;
NSLog(#"%f", x);
the output is 52.450001 and not 52.450000 !
I don't know because this happens!
Thanks for any help!
~SOLVED~
Thanks to everybody! Yes, I've solved with the double type!
Floats are a number representation with a certain precision. Not every value can be represented in this format. See here as well.
You can easily think of why this would be the case: there is an unlimited number of number just in the intervall (1..1), but a float only has a limited number of bits to represent all numbers in (-MAXFLOAT..MAXFLOAT).
More aptly put: in a 32bit integer representation there is a countable number of integers to be represented, But there is an infinite innumerable number of real values that cannot be fully represented in a limited representation of 32 or 64bit. Therefore there not only is a limit to the highest and lowest representable real value, but also to the accuracy.
So why is a number that has little digits after the floating point affected? Because the representation is based on a binary system instead of a decimal, making other numbers easily represented then the decimal ones.
See http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
Floating point numbers can not always be represented easily by computers. This leads to inaccuracy in some digits.
It's like me asking you what 1/3 is in decimal. No matter how hard you try, you're not going to be able to tell me what it is because decimal can't accurately describe that number.
Floats can't accurately describe some decimal numbers.