Re: How do you round off a float?

From: Walter (walter_at_digitalmars.nospamm.com)
Date: 01/02/05


Date: Sun, 02 Jan 2005 19:45:43 GMT


<kdlittle88@yahoo.com> wrote in message
news:1104652353.929533.225920@z14g2000cwz.googlegroups.com...
> Does anyone have any sample code on how to round off a float to x
> digits? say for example rounding 3.1415 to 3.1?
>
> Any help would be appreciated. I am learning C++ and have not found
> this info on the net (though it might be out there)

o Floating point formats are almost always in binary, not in decimal.
Many decimal numbers cannot be exactly represented by the underlying
floating point format - 3.1 is an example. Rounding to a certain number of
binary digits is very different from rounding to a certain number of decimal
digits.

o Rounding of floating point values should only done with the final
result for purposes of presenting to humans. Rounding intermediate values is
a bad idea, only useful for demonstrating how badly it can trash your
results. Why this is true is not so easy to grasp, many smart people have
fallen victim to misunderstanding the need to use all the precision
available. Check out Prof. Kahan's work on this (google for "Kahan floating
point").

o A corollary to the above rule is use the maximum precision available on
your machine. For x86, that would be 80 bit long doubles. (Unfortunately,
some popular C++ compilers do not support 80 bit long doubles, they max out
at 64. Digital Mars C++ supports 80 bit long doubles.) You can get away with
lesser precision if speed and memory consumption are paramount and accurate
results are not (such as in game graphics engines).

o Rounding to a certain number of digits for display of the final result
is handilly performed by the 'precision' field in the printf format family
of functions. For example,
    printf("%.8g\n", d);
prints d with 8 digits of precision. Note the decimal point before the 8.

Hope this helps!

-Walter
www.digitalmars.com free C, C++, D compilers
"code of the nerds"



Relevant Pages

  • Re: float bug? perl 5.8, DBI and oracle 10.2.0
    ... precision numbers in oracle, you've got 38 decimal digits to play ... and with minimal coaxing perl will handle them as ... digits from a 32 bit floating point number - I'll go out on a limb ... and hazard that one can expect 12 or so digits from a 64 bit floating ...
    (perl.dbi.users)
  • Re: Linear Algebra Challenge
    ... Since I'm using floating point, so I'll never be able to calculate one ... floating point math set to 99 digits. ... As close as I'm willing to wait if I use arbitrary precision. ... This mode is fast; when you select arbitry ...
    (comp.sys.hp48)
  • Re: Interesting math
    ... Floating point number represents a real number with 6 digits precision. ... Floating point numbers are denoted by the keyword float. ...
    (alt.usage.english)
  • Re: Interesting math
    ... Floating point number represents a real number with 6 digits precision. ... Floating point numbers are denoted by the keyword float. ...
    (alt.usage.english)
  • Re: Rounding errors
    ... >There is no 'error' in rounding. ... >precision numbers and to give an accurate representation in a limited ... >When that set is rounded to 2 digits of precision it only needs to ... >truncation does not represent. ...
    (comp.lang.cobol)