Reputation: 13377
I get different results when try to division same numbers in PHP and C#:
PHP:
echo 1000/0.03475;
output:28776.9784173
C#:
1000/0.03475
output:28776,9784172662
Why? And how to get same results?
Upvotes: 0
Views: 401
Reputation: 3512
There are two possibilities, as others have said: either they're the same floating-point value, but just printing differently, or they're different floating-point values. To see if they're the same, you need to print all the decimal digits -- if your language allows (C# and PHP don't -- see my article http://www.exploringbinary.com/print-precision-of-dyadic-fractions-varies-by-language/).
They could be different floating-point values because not all compilers/interpreters/run-time libraries convert every decimal string to the correct floating-point number (in your example, I'm talking about the 0.03475 part in particular). See my article http://www.exploringbinary.com/incorrectly-rounded-conversions-in-visual-c-plus-plus/ for more details.
Upvotes: 0
Reputation: 97835
What the other answers say is probably the reason you have different values (more accurately, different rounding in display).
However, in PHP a float as the same semantincs a C double
(it usually has 64 bits) has (additionally, PHP targets C89, while only C99 defined floating point arithmetic so that it complies with IEEE 754):
The C89 standard does not even require IEEE format, and for C99, full IEEE 754 support (Inf, NaN, denormals) is optional.
In C# a float
is 32-bit wide and is guaranteed to be (mostly) IEEE 754 compliant. The closest thing to PHP's float would be C# double
.
Upvotes: 1
Reputation: 147451
But they do give the very same result, just rounded to different numbers of decimal places when displayed. If you were to look at the actual bits representing the floating-point number in memory, they ought to be identical (given the same architecture).
Upvotes: 4
Reputation: 799230
Because that's how they're being displayed.
printf("%.10f\n", 1000 / 0.03475);
Upvotes: 0