Discussion:
Rounding float point numbers using C/C++ library
(too old to reply)
Jack
2009-09-23 08:40:59 UTC
Permalink
I'd like to round some floating point numbers with 2 decimal places
precision. For example 12.5521 => 12.55. I want to discard/truncate the
third and forth and all after that decimal places. Are there any C/C++
libraries that do this? Don't want to roll my own cos too lazy :)
Thanks
Jack
Jack
2009-09-23 08:45:05 UTC
Permalink
Better say "round off"...
Thanks
Jack
Victor Bazarov
2009-09-23 11:58:38 UTC
Permalink
Post by Jack
I'd like to round some floating point numbers with 2 decimal places
precision. For example 12.5521 => 12.55. I want to discard/truncate the
third and forth and all after that decimal places. Are there any C/C++
libraries that do this? Don't want to roll my own cos too lazy :)
double round(double a, unsigned digits_after)
{
double multiplier = pow(10, digits_after);
return int(a * multiplier + 0.5) / multiplier;
}

(verify how it works with negative numbers, fix if necessary)

Beware, however, that just like with any other arithmetic operations,
you might get a slightly imprecise result. 12.55 cannot be exactly
represented in computers using binary form, so watch out for the values
like 12.5499999999999999.

Perhaps you can explain why you want to round those numbers. If you're
trying to come up with a solution for handling money, then you need to
consult books written about implementing accounting. IIRC, you actually
don't want to use fractions when dealing with money, and instead
represent your amounts in the lowest denomination (cent, pfennig, grosz,
kopeck) and not currency (dollars, mark, zloty, ruble). If you're
trying to create a better output, then using proper format would take
away the need to round the number (RTFM on 'fprintf' or 'ostream').

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jack
2009-09-23 12:08:35 UTC
Permalink
[snip]
Post by Victor Bazarov
double round(double a, unsigned digits_after)
{
double multiplier = pow(10, digits_after);
return int(a * multiplier + 0.5) / multiplier;
}
(verify how it works with negative numbers, fix if necessary)
Beware, however, that just like with any other arithmetic operations, you
might get a slightly imprecise result. 12.55 cannot be exactly
represented in computers using binary form, so watch out for the values
like 12.5499999999999999.
Perhaps you can explain why you want to round those numbers. If you're
trying to come up with a solution for handling money, then you need to
consult books written about implementing accounting. IIRC, you actually
don't want to use fractions when dealing with money, and instead represent
your amounts in the lowest denomination (cent, pfennig, grosz, kopeck) and
not currency (dollars, mark, zloty, ruble). If you're trying to create a
better output, then using proper format would take away the need to round
the number (RTFM on 'fprintf' or 'ostream').
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Hello Victor,
Thanks for your help. Actually I am working on a Direct3D plugin (export)
for 3ds max. I am doing some ray casts that imprecise numbers won't hit. So
that I have to round off.
Thanks a lot really
Jack
Victor Bazarov
2009-09-23 12:25:22 UTC
Permalink
Post by Jack
[..]
Thanks for your help. Actually I am working on a Direct3D plugin (export)
for 3ds max. I am doing some ray casts that imprecise numbers won't hit. So
that I have to round off.
You have to understand that 12.55 is no more precise than 12.5521, AFA
arithmetic on the FPU is concerned. Only integral values and fractions
that are [combinations of] powers of 2 can be represented precisely.
Any other number will be represented with approximation. You need to
figure out what "precision" means for your algorithm. Perhaps you need
to round to the nearest power of 2, or maybe you simply need to round to
the nearest 'float'.

Ask the folks in 'comp.graphics.algorithms', they might know more about
the "precision" and what it means AFA Direct3D is concerned.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Guido Franzke
2009-09-23 13:02:36 UTC
Permalink
Hi.
What's the news server address of comp.graphics.algorithms? (For MS I have
news.microsoft.com)
I want to see it in Outlook Express.
Regards, Guido
Post by Victor Bazarov
Ask the folks in 'comp.graphics.algorithms',
Martin T.
2009-09-23 13:22:00 UTC
Permalink
Post by Guido Franzke
Hi.
What's the news server address of comp.graphics.algorithms? (For MS I have
news.microsoft.com)
I want to see it in Outlook Express.
Use news.eternal-september.org
(It's completely free - you just have to register an account)
Jack
2009-09-24 12:12:17 UTC
Permalink
I wonder if they have free UNIX shell account as well
:)
Thanks
Jack

Continue reading on narkive:
Loading...