Re: Deviation of double
From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 05/17/04
- Next message: Karl Heinz Buchegger: "Re: Deviation of double"
- Previous message: Victor Bazarov: "Re: Deviation of double"
- In reply to: Christian Meier: "Re: Deviation of double"
- Next in thread: Karl Heinz Buchegger: "Re: Deviation of double"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 17 May 2004 10:32:37 -0400
Christian Meier wrote:
> "Karl Heinz Buchegger" <kbuchegg@gascad.at> schrieb im Newsbeitrag
> news:40A8BEC4.A72FD084@gascad.at...
>
>>Christian Meier wrote:
>>
>>>Hallo NG
>>>
>>>My problem is the deviation of the floating point datatypes. 90.625 is
>
> not
>
>>>exactly 90.625. It is 90.624999999.... on my system. Now I tried to find
>
> a
>
>>>solution for my problem but I couldn't think of one.
>>
>>Forgive me. What was your problem?
>>
>>
>>>So I tried to write a
>>>work-arround which should solve the problem for the most numbers.
>>>What do you think of this: (I know it is not a perfect solution....)
>>
>>I doesn't look like solution to anything.
>>
>>--
>>Karl Heinz Buchegger
>>kbuchegg@gascad.at
>
>
> Sorry, wasn't a really good description of my problem. I will give more
> infos: I have a function with two parameters. One of type double, the other
> one is an int.
> string func (double dValue, int iDigits);
> This function must return a string which represents the double value.
> iDigits is the number of digits after the point. For this I use sprintf. And
> now I got a problem. When passing 90.625 to the function (iDigits = 2) then
> the function returned 90.62 instead of 90.63. I found out that the double is
> stored as 90.624999999... on my system. So I need a solution for this. The
> function has to return 90.63. So I thought I can add a small value to dValue
> before rounding. And the double has a limit of 15 decimal digits on my
> system. So I tried to write a work-arround as described in my first post.
> Can you now understand my problem?
I believe you're confused about rounding. 90.6249999999999 simply _must_
round to 90.62 if only two decimal digits are required. That's how
rounding is defined. You cannot assume that "since it's so close to
90.625, I should begin my rounding from 90.625 (instead of the actual value)".
If your function "has to return 90.63", add this statement:
return 90.63;
to your function. If, OTOH, you want it to work correctly, analyse the
problem further. Why do you get 90.6249999999999 instead of [expected]
90.625? Why are you expecting 90.625? What values do you start with?
What operations are you performing on those values? And so on...
Victor
- Next message: Karl Heinz Buchegger: "Re: Deviation of double"
- Previous message: Victor Bazarov: "Re: Deviation of double"
- In reply to: Christian Meier: "Re: Deviation of double"
- Next in thread: Karl Heinz Buchegger: "Re: Deviation of double"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|