Hi
Does any body know, how to round a double value with a specific number
of digits after the decimal points?
A function like this:
RoundMyDouble (double &value, short numberOfPrecisions)
It then updates the value with numberOfPrecisions after the decimal
point.
Any help is appreciated.
Thanks.
md
Nov 22 '07
206 13007
James Kuyper <ja*********@verizon.netwrites:
Bart wrote:
...
>Not many posting here seem to believe there are real and practical reasons for rounding values to so many decimals (or rounding to a nearest fraction, a related problem).
Incorrect. What I believe is that the real and practical reasons tend
to fall into two categories:
a) Conversion of floating point numbers to digit strings, usually for
output.
b) Calculations that should, properly, be carried out in fixedpoint
arithmetic. In the absence of direct language support for fixedpoint,
it should be emulated by the programmer using, for instance, an
integer to represent 1000 times the actual value, if that value is to
be stored with 3 digits after the decimal place. All of the example
you gave should fall into this second category.
[...]
Yes, there are good reasons for wanting to round a numeric value to a
specified number of decimal places, but neither example you provided
(nor any other reasonable example I can think of) calls for storing
the resulting rounded value in a double.

Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
 Antony Jay and Jonathan Lynn, "Yes Minister"
On Nov 27, 7:11 am, Richard Heathfield <r...@see.sig.invalidwrote:
There are many reasons why this kind of rounding is useful in the real
world, often to deal with noise reduction.
We don't call that "rounding", though. We call it "approximating". Nobody
denies its utility.
I think Keith Thompson and one or two others were.
>But rounding it ain't.
I'm intrigued, why are you so prejudiced about poor old round(x)
function for reasons that could be applied to nearly anything, like
(slightly contrived) a reciprocal(x) function for example? In fact in
seems Divide is the real culprit because often you don't get exactly
what it says on the box.
Bart
Bart said:
On Nov 27, 7:11 am, Richard Heathfield <r...@see.sig.invalidwrote:
There are many reasons why this kind of rounding is useful in the real
world, often to deal with noise reduction.
We don't call that "rounding", though. We call it "approximating". Nobody denies its utility.
I think Keith Thompson and one or two others were.
>>But rounding it ain't.
I'm intrigued, why are you so prejudiced about poor old round(x)
function for reasons that could be applied to nearly anything, like
(slightly contrived) a reciprocal(x) function for example?
I'm not sure that "prejudiced" is the right word. I don't /use/ it, but
that's only for the same reason that I don't use any C99 features in code
intended to be portable. But round() doesn't actually round to a given
number of decimal places (or rather, it does, provided that the number of
decimal places you want is zero!).
If you try to use it to round 0.33 to one decimal place, e.g. like this:
f = round(f * 10) / 10;
then you'll get 0.30000000000000000001 or 0.29999999999999998 or something
like that. What you won't get is *precisely* 0.3 in f.
In fact in
seems Divide is the real culprit because often you don't get exactly
what it says on the box.
Whatever the culprit, the crime remains  you can't (in the general case)
store an arbitrary value in a double to precisely n decimal places, for
any value of n 0. (You *can* store particular values, of course: 0.5,
0.75, and so on.)

Richard Heathfield <http://www.cpax.org.uk>
Email: http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place"  dmr 29 July 1999
Richard Heathfield wrote:
Bart said:
....
>I'm intrigued, why are you so prejudiced about poor old round(x) function for reasons that could be applied to nearly anything, like (slightly contrived) a reciprocal(x) function for example?
I'm not sure that "prejudiced" is the right word. I don't /use/ it, but
that's only for the same reason that I don't use any C99 features in code
intended to be portable. But round() doesn't actually round to a given
number of decimal places (or rather, it does, provided that the number of
decimal places you want is zero!).
I suspect that he was not talking about the C99 round(); it sounded more
like he was unaware of it's existence. round() can calculate it's result
exactly, at least for it's typical argument values.
I think he using "round()" as a name for the kind of function we've been
talking about in this thread. It takes a double value, and an integer
number of digits, and returns the best possible approximation to the
provided value rounded to the specified number of digits after the
decimal place. For most typical argument values this function cannot
return exactly the mathematical value we would like it to return. In
that regard, it is no different from most other operations on floating
point values, or most other functions taking floating point arguments.
If you try to use it to round 0.33 to one decimal place, e.g. like this:
f = round(f * 10) / 10;
then you'll get 0.30000000000000000001 or 0.29999999999999998 or something
like that. What you won't get is *precisely* 0.3 in f.
For the same reason, reciprocal(5.0) can't give you precisely 0.2. So?
Why is one inaccuracy acceptable, and the other is not? Or are you
suggesting that they're both unacceptable?
James Kuyper said:
Richard Heathfield wrote:
<snip>
>If you try to use it to round 0.33 to one decimal place, e.g. like this:
f = round(f * 10) / 10;
then you'll get 0.30000000000000000001 or 0.29999999999999998 or something like that. What you won't get is *precisely* 0.3 in f.
For the same reason, reciprocal(5.0) can't give you precisely 0.2. So?
Why is one inaccuracy acceptable, and the other is not? Or are you
suggesting that they're both unacceptable?
This was well answered elseperson elsethread. Let me see if I can find it.
Ah, here we go: MessageID: <fi**********@aioe.org>
in which Keith writes:
"But there's a fundamental difference between sqrt() and a function
that purports to round a floatingpoint value to a specified number
of decimal places.
"For the sqrt() function, obtaining a close approximation to the
mathematical result is obviously a useful thing to do. There are
some contexts in which you might want to throw up your hands and
say "Sorry, it's not possible to compute sqrt(2.0) exactly"  but
obtaining a close approximation is both useful and expected.
"For the rounding function, sure, you could write a function that,
given arguments (3.14159, 2) would return the closest floatingpoint
approximation of the mathematical value 3.14. But it's not at all
clear either that this would be useful, or that it's really what the
original poster wants."

Richard Heathfield <http://www.cpax.org.uk>
Email: http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place"  dmr 29 July 1999
On Nov 27, 2:33 pm, Richard Heathfield <r...@see.sig.invalidwrote:
James Kuyper said:
For the same reason, reciprocal(5.0) can't give you precisely 0.2. So?
This was well answered elseperson elsethread. Let me see if I can find it.
in which Keith writes:
"But there's a fundamental difference between sqrt() and a function
that purports to round a floatingpoint value to a specified number
of decimal places.
So, the thing about rounddouble(x,n) is that it always gives an exact
result in decimal arithmetic, compared with reciprocal(x) (sometimes
exact) or sqrt(x) (rarely exact)?
Due to dividing by 10^n which is no problem in decimal but causes
grief in binary?
OK. But back in the practical world, it can still be useful to class a
rounddouble(x) function with sqrt, reciprocal and the like provided
the limitations are known.
Bart.
Bart said:
<snip>
OK. But back in the practical world,
I rarely leave it, which is why I think it's so important to get things
right.
it can still be useful to class a
rounddouble(x) function with sqrt, reciprocal and the like provided
the limitations are known.
Of course it can be useful, and nobody denies this  provided, as you say,
the limitations are known. That is why I have laid such stress on pointing
out the limitations (which, it seemed to me, the phrasing of the OP's
question suggested that he didn't know).

Richard Heathfield <http://www.cpax.org.uk>
Email: http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place"  dmr 29 July 1999 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dalan 
last post by:
Is there any code available to address currency rounding problems in
Access 97? Apparently, selecting currency type table fields does not
resolve the problem. For instance, in my form I have a...

by: Norvin Laudon 
last post by:
Hi,
Can somebody explain the following, from the MSDN documentation for the
"System.Convert.ToInt32(double)" function
<quote>
Return Value
value rounded to the nearest 32bit signed...

by: Jason 
last post by:
I am having a rounding problem in a value i am trying to display in VB.NET.
If I have the following code:
Dim x As Single = 2726.795
Dim y As Single = Math.Round(2726.795, 2)
Dim s as String =...

by: Marco 
last post by:
Hello,
I have :
float f = 36.09999999;
When I do :
char cf;
sprintf(cf,"%0.03lf", f);
I get : 36.100

by: Shirsoft 
last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding
error in the 8th digit. Unfortunately because of the algorithm we use,
the errors percolate into higher digits.
C++ code is...

by: abcd 
last post by:
I am trying to write a rounding function. Rounding to 0.05. e.g.
I should get below results
6.125 6.15
1.699 1.7
1.1985 1.20
0.5625 0.60
Can someone have any sample for...

by: Spoon 
last post by:
Hello everyone,
I don't understand how the lrint() function works.
long lrint(double x);
The function returns the nearest long integer to x, consistent with the
current rounding mode. It...

by: md 
last post by:
Hi
Does any body know, how to round a double value with a specific number
of digits after the decimal points?
A function like this:
RoundMyDouble (double &value, short numberOfPrecisions)
...

by: jacob navia 
last post by:
Hi
"How can I round a number to x decimal places" ?
This question keeps appearing. I would propose the following
solution
#include <float.h>
#include <math.h>

by: abbasky 
last post by:
### Vandf component communication method one: data sharing
Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...

by: fareedcanada 
last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...

by: davi5007 
last post by:
Hi,
Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

by: MeoLessi9 
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

by: DolphinDB 
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...

by: DolphinDB 
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion highfrequency records to 61 million...

by: Aftab Ahmad 
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

by: Aftab Ahmad 
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
 