|
Posted by Gordon on 01/03/08 09:24
On Jan 3, 5:51 am, Lawrence <lawb...@gmail.com> wrote:
> On Jan 3, 10:34 am, Michael Fesser <neti...@gmx.de> wrote:
>
> > .oO(Lawrence)
>
> > >hi all, while im computing large values the variable value gets
> > >rounded off to the next higher value .How do we stop or prevent this
> > >happening.
>
> > How large is "large"? What about a code example?
>
> > Micha
>
> $a=$b+$c+$d
> where $b=99999999999999
> $c=99999999999999
> $d=99999999999999
The problem you are running into is integer overflow. a 32 bit
integer can hold values of -~ 2 billion to +~2 billion, or 0 - ~4
billion depending on whether or not it is a signed integer. Your sum,
while the inputs are integers, will result in an answer that is too
big to fit into an integer. PHP has dynamic types so it will switch
to a floating point representation in this case. Floating point
numbers cannot exactly represent some numbers, just like you can't
represent 1/3 in decimal. Instead, the floating point result will
contain the closest approximation to the actual number instead.
In this case, what you need is not a floating point number, but what
is called a bignum instead. Bignums chain smaller integers, or
strings depending on implementation, together in order to represent
larger values. PHP provides a BCMath extension that includes support
for bignums, look at the BCMath documenatation on php.net.
Navigation:
[Reply to this message]
|