02.01.2016, 22:01
If you use arithmetic with different data types the "weaker" data type will be converted
Now if you divide a Float with an Integer, the Integer will be converted to a Float, resulting in a Float
But if you do a simple Integer division you get as result an integer
Now if you divide a Float with an Integer, the Integer will be converted to a Float, resulting in a Float
But if you do a simple Integer division you get as result an integer
PHP код:
printf("%f", 5.0 / 2.0); // float / float = float => 2.5
printf("%f", 5.0 / 2); // float / int = float / float(int) = float => 2.5
printf("%f", 5 / 2.0); // int / float = float(int) / float = float => 2.5
printf("%d", 5 / 2); // int / int = int => 2 (the .5 gets chopped away)