How do you round off a float?

K

kdlittle88

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)

tiny k
 
M

Mike Hewson

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)

tiny k

Integer casts will truncate to the right of the decimal point.
This suggests moving the point back and forth.

*pseudocode*

declare somenumber; // 3.1415
declare someprecision; // x, say equals 1.

shifter = 10_to_the_power_of( someprecision );
shift_it = somenumber*shifter;
truncate_it = integer_part_of( shift_it );
shift_it_back = truncate_it/shifter; // Result.

*end pseudocode*
 
D

David Harmon

On 1 Jan 2005 23:52:33 -0800 in comp.lang.c++, (e-mail address removed)
wrote,
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?

You do it when you format for display.
cout << setprecision(2) << pi;

Never expect an exact floating point value. Typical binary floating
point implementation cannot even represent 3.1 exactly, so forget
"rounding".

Also, prefer double over float unless you have a specific reason.
 
W

Walter

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"
 
K

kdlittle88

It did! I forgot about printf. My code looks like this now and seems
to work just fine!

num = num + .05;
printf = ("rounded number: %4.1f", num);

Thanks for the help!

p.s. does digital mars fit on a 64 meg flash disk and does it run
without having to be installed? I am currently using djgpp but find it
is a bit less user friendly than what I would like.

Thanks
tiny k
 
M

Mike Wahler

It did! I forgot about printf. My code looks like this now and seems
to work just fine!

num = num + .05;
printf = ("rounded number: %4.1f", num);

Thanks for the help!

p.s. does digital mars fit on a 64 meg flash disk and does it run
without having to be installed? I am currently using djgpp but find it
is a bit less user friendly than what I would like.

Visit the digitalmars.com web site to find out these things,
which are not language issues.

-Mike
 
K

kdlittle88

I posted a language issue. I got a reply that spurred another
question, and I don't see another way to contact the person (walter)
who is FROM digital mars and I don't see a reason why I have to find
another way to do it. If you want to start a flame war, feel free.
In the meantime my question for WALTER is still valid.
tiny k
 
M

Mike Hewson

I posted a language issue. I got a reply that spurred another
question, and I don't see another way to contact the person (walter)
who is FROM digital mars and I don't see a reason why I have to find
another way to do it. If you want to start a flame war, feel free.
In the meantime my question for WALTER is still valid.
tiny k

Yo there! Relax man! Staying nice is the way to get help here. :)

Please note: at the bottom of http://www.digitalmars.com/ is a button
that says 'send email to Digital Mars'. It's likely Walter will get it.
 
W

Walter

It did! I forgot about printf. My code looks like this now and seems
to work just fine!

num = num + .05;
printf = ("rounded number: %4.1f", num);

Thanks for the help!

Sure! You don't need to add the .05, the printf formatter will do the "add
half and round" for you (or it should).
p.s. does digital mars fit on a 64 meg flash disk

Yes. The free download for it is 3 Mb.
and does it run without having to be installed?

Yes (though you do need to unzip it!).

-Walter
www.digitalmars.com free C, C++, D compilers
 
M

Mike Wahler

I posted a language issue.

Yes, originally.
I got a reply that spurred another
question,

If you're talking about the questions about operating
the DM compiler, those are not topical here.
and I don't see another way to contact the person (walter)
who is FROM digital mars

"Don't see another way", does not make these issues topical.
Have you tried checking the DM site for support contacts?
and I don't see a reason why I have to find
another way to do it.

A very good reason is to prevent alienating yourself
from the folks here who can help you.
If you want to start a flame war, feel free.

No flame war wanted or needed.
In the meantime my question for WALTER is still valid.

It's not topical here, regardless of whether you call it
'valid' or not.

Please see:
http://www.slack.net/~shiva/welcome.txt

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top