Ok #php folks. I have puzzle for you i'm curious about.

ceil( 9.97 * 100 ) = 998

but

ceil( 997 ) = 997

and

round( 9.97 * 100 ) = 997

WHYYYYYYY????

Follow

@codemonkeymike

Going to call them case #1, case #2 and case #3.

Within the ()'s of case #1 and case #3, you are multiplying a floating point number by an integer.

FP#'s are represented in binary, not decimal, so it is NOT EXACTLY 9.97 but just an infinitesimal bit more or less. Doesn't usually matter, but technically it's an approximation, let's say maybe 9.97000000000000004581.

(FP * integer) product result is always promoted to an FP. So then in my example, product would be 997.00000000000000458.

`ceil` (which results in an integer) takes that to 998. `round` takes it to 997, because 997.00000000000000458 is less than 997.5.

Unless a FP can be expressed in it's binary format exactly (examples: 3.5, 4.125, 7.50625) as a fraction with a power of 2 denominator(same examples: 7/2, 33/8, 121/16), it is always an approximation.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.