5

When I debug my software in VS C++ by stepping the code I notice that some float calculations show up as a number with a trailing dot, i.e.:

1232432.

One operation that lead up to this result is this:

float result = pow(10, a * 0.1f) / b

where a is a large negative number around -50 to -100 and b is most often around 1. I read some articles about problem with precision when it comes to floating-points. My question is just if the trailing dot is a Visual-Studio-way of telling me that the precision is very low on this number, i.e. in the variable result. If not, what does it mean?

This came up at work today and I remember that there was a problem for larger numbers so this did to occur every time (and by "this" I mean that trailing dot). But I do remember that it happened when there was seven digits in the number. Here they wright that the precision of floats are seven digits:

C++ Float Division and Precision

Can this be the thing and Visual Studio tells me this by putting a dot in the end?

I THINK I FOUND IT! It says "The mantissa is specified as a sequence of digits followed by a period". What does the mantissa mean? Can this be different on a PC and when running the code on a DSP? Because the thing is that I get different results and the only thing that looks strange to me is this period-thing, since I don't know what it means.

http://msdn.microsoft.com/en-us/library/tfh6f0w2(v=vs.71).aspx

Community
  • 1
  • 1
Thomas Johansson
  • 583
  • 2
  • 6
  • 15
  • 1
    FYI `1.` and `.1` are valid double literals in C++ (at least gcc) – Joe Dec 15 '11 at 21:36
  • I get that .1 are a valid float/double but why does it not print the same trailing . for every float? If a double is zero it prints 0.00000 but nevery 0. only. Why is it trailing dot only a couple of time, it must be something special that occurs for some large numbers. – Thomas Johansson Dec 15 '11 at 22:08
  • @ThomasJohansson: You'll need to take that up with the VS team I suppose. The point is that it has absolutely nothing to do with the precision of a float. Here, you should know [this stuff](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). – Ed S. Dec 15 '11 at 22:21

3 Answers3

4

If you're referring to the "sig figs" convention where "4.0" means 4±0.1 and "4.00" means 4±0.01, then no, there's no such concept in float or double. Numbers are always* stored with 24 or 53 significant bits (7.22 or 15.95 decimal digits) regardless of how many are actually "significant".

The trailing dot is just a decimal point without any digits after it (which is a legal C literal). It either means that

  • The value is 1232432.0 and they trimed the unnecessary trailing zero, OR
  • Everything is being rounded to 7 significant digits (in which case the true value might also be 1232431.5, 1232431.625, 1232431.75, 1232431.875, 1232432.125, 1232432.25, 1232432.375, or 1232432.5.)

The real question is, why are you using float? double is the "normal" floating-point type in C(++), and float a memory-saving optimization.

* Pedants will be quick to point out denormals, x87 80-bit intermediate values, etc.

dan04
  • 87,747
  • 23
  • 163
  • 198
  • Thanks! I already found the original problem and managed to solve this but this is a good explanation! – Thomas Johansson Jan 08 '12 at 15:10
  • Tnx! I saw this trailing dot here, which if i'm not mistaken, is accordingly used as a casting mechanism to int: int i = 10. * sqrt(n) – Makan Dec 28 '14 at 11:48
3

The precision is not variable, that is simply how VS is formatting it for display. The precision (or lackof) is always constant for a given floating point number.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 6
    Though a trailing dot may be a hint that the number is a floating point number rather than an integer. – Brian Dec 15 '11 at 21:47
  • @Brian: I think that's why it's there, just so happens to have all 0's after the decimal. – Ed S. Dec 15 '11 at 21:48
  • Because it does not print a dot at the end of any floating-point and it does not print it for the same floating-point every time so it must be something special that occurs a some point. For one calculation I tried to calculate the value with the Windows calculator and it differed... – Thomas Johansson Dec 15 '11 at 22:05
  • Windows Calculator uses `double` (older versions) or decimal arithmetic (newer versions), not `float`. – dan04 Dec 15 '11 at 22:16
  • That does not really answer the question. – Thomas Johansson Dec 15 '11 at 22:23
  • @ThomasJohansson: What is the question exactly? The precision question has been answered, what is left? – Ed S. Dec 15 '11 at 22:25
1

The MSDN page you linked to talks about the syntax of a floating-point literal in source code. It doesn't define how the number will be displayed by whatever tool you're using. If you print a floating-point number using either printf or std:cout << ..., the language standard specifies how it will be printed.

If you print it in the debugger (which seems to be what you're doing), it will be formatted in whatever way the developers of the debugger decided on.

There are a number of different ways that a given floating-point number can be displayed: 1.0, 1., 10.0E-001, and .1e+1 all mean exactly the same thing. A trailing . does not typically tell you anything about precision. My guess is that the developers of the debugger just used 1232432. rather than 1232432.0 to save space.

If you're seeing the trailing . for some values, and a decimal number with no . at all for others, that sounds like an odd glitch (possibly a bug) in the debugger.

If you're wondering what the actual precision is, for IEEE 32-bit float (the format most computers use these days), the next representable numbers before and after 1232432.0 are 1232431.875 and 1232432.125. (You'll get much better precision using double rather than float.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631