|
Posted by Anthony Tippett on 04/05/05 03:23
btw, thanks for your response.
I'm not sure as if I understand why. It's not like I'm using a very
precise number when dealing with the hundreths place.
Even without the multiplication the number gets messed up.
eg.
$a = 17.00;
$a+= 1.10;
$a+= 0.32;
$a+= 0.07;
print $a."<br>"; // 18.49
var_dump($a); // float(18.49)
var_dump($a-18.49); // float(3.5527136788005E-15)
I'm just trying to add money amounts? Can I not rely on floats to do this?
Richard Lynch wrote:
> Floats are NEVER going to be coming out "even" reliably.
>
> You'll have to check if the difference is less than X for whatever number
> X you like.
>
> Or you can look at something like BC_MATH where precision can be carried
> out as far as you like...
>
> But what you are seeing is to be expected.
>
> That's just the way computers work, basically.
>
> On Mon, April 4, 2005 5:07 pm, Anthony Tippett said:
>
>>Ok i've narrowed it down a little bit but still can't figure it out..
>>
>>Here's the code and what I get for the output. Does anyone know what's
>>going on? Can someone else run it on their computer and see if they get
>>the same results?
>><?php
>>
>>$a = "17.00" * "1";
>>$a+= "1.10" * "1";
>>$a+= "0.32" * "1";
>>$a+= "0.07" * "1";
>>
>>print $a."<br>"; // 18.49
>>
>>var_dump($a); // float(18.49)
>>var_dump($a-18.49); // float(3.5527136788005E-15)
>>?>
>>
>>
>>Anthony Tippett wrote:
>>
>>>I'm having trouble figuring out why subtraction of two floats are giving
>>>me a very small number. I'm thinking it has something to do with the
>>>internals of type casting, but i'm not sure. If anyone has seen this or
>>>can give me some suggestions, please.
>>>
>>>I have 2 variables that go through a while loop and are
>>>added/subtracted/ multipled. InvAmt and InvPay are shown as
>>>floats but when they are subtracted, they give me a really small
>>>number
>>>
>>>// code
>>>var_dump($InvAmt);
>>>var_dump($InvPay);
>>>var_dump($InvAmt-$InvPay);
>>>
>>>// output
>>>float(18.49)
>>>float(18.49)
>>>float(2.1316282072803E-14)
>>>
>>
>>--
>>PHP General Mailing List (http://www.php.net/)
>>To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
>
>
Navigation:
[Reply to this message]
|