13 comments

3

The answer is yes. You can represent any number between 10^37 and 10^-39 to within 7 significant figures if using a 32 bit float. Using a 23 bit mantissa gives the smallest representable change in value of 1/(2^23)2^exponent for a given exponent. The largest value that a float can represent is approximately (2(2^exponent)) and divide that by the smallest representable change you get 2/(1/2^23) and take the log base 10 to find out how many decimal digits are significant which comes out to 7.2247 significant digits

See here for the significant figures for other data types. https://en.wikipedia.org/wiki/Floating_point#Internal_representation

-1

But I can't, and neither can you. What am I supposed to tell my readers about the ranges? (I'm writing a tiny book) and readers will see the ranges for short, ...long, and then comes floating point with a little paragraph on the side that says 3.14, don't push it or you'll get errors?

EDIT Hold on, maybe I can... testing some more...

1

Uh, can someone ELI5 this? What are significant digits.

I feel like whenever a programming post hits the front page there should be a ELI5 if possible.

1

What are significant digits.

Basically digits that are considered to be meaningful or valid

0

https://www.youtube.com/watch?v=eCJ76hz7jPM -best resource for significant figures.

0

I don't know what you are asking. But using floating points means you lose accuracy every time you do a calculation. And you lose more accuracy the bigger your number becomes.

You can increase accuracy by reducing the number of operations on the float.

float   a=4.59
float   b=a*2.0*2.0*2.0 
float   c=b=a*8.0

In the example above "b" will have a lower accuracy then "c".

Also comparing a floating point value comparing it against an smallest value epsilon.

if (a==1.0) {
  // never works
}

but this dies:

if (ABS(a-1.0)<0.001) {
  // always works
}
0

You don't really lose accuracy every time you do a calculation. The numbers floating point can represent, it represents with 100% accuracy. Calculations on numbers it represents accurately, where it also represents the result accurately, won't magically make the result magically less accurate. It's just that it can't represent all numbers, including some which seem trivial in base 10, like 1.1, because they have an infinite or very large number of digits in base 2.

0

I don't know what you are talking about. Every time you perform a calculation you get some unpresentable result and you have to round to the next closest float and since that float isn't the exact result you lose accuracy.

I don't know how in one sentence you can say it doesn't lose accuracy every time you do a calculation and then say that it can't represent every result in the very next statement. Unless you are doing operations where your operands are using the same exponent you're results are NEVER going to be some value that a float can represent and you will ALWAYS have some error.

Just take a look at Wikipedia if you need some examples on the horrible errors that you can get with floats. https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

1

@mort is right. Some numbers can be represented with perfect accuracy, and if the result is such a number then you don't lose accuracy. For example, 0.125 * 8 always equals 1 because 0.125, 8 and the result can all be represented perfectly as floats. That is the exception though, in most cases accuracy is lost.

-1

The numbers floating point can represent, it represents with 100% accuracy.

With floats it is not. The accuracy depends on how big that number is (1.0 is more accurately stored than 1000.0.) but never 100%

Calculating a=1.0 x 1000.0 will be less precise that a=1.0 x 10.0

https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

If accurate numbers is required you avoid floats that is why the invented other none-float numbers like BigInteger, ....

0

It's nice to see how you get downvoted for being correct.

-1

The software industry, tons of big egoed people that think that they are more intelligent than others while in reality their intelligence is based in superstition.

2

That's not a good example because multiplying by a power of two just bumps the exponent and doesn't change the mantissa, so no accuracy is lost unless it overflows. The expressions 4.59 * 8 and 4.59 * 2.0 * 2.0 * 2.0 will always be equal. A better example would be 4.59 * 1.331 which is more accurate than 4.59 * 1.1 * 1.1 * 1.1.