4 ms·
Knowing how to use a language is critical to get expected results. This gives the proper result in PHP by forcing the integer cast. $sum = (int) $sum + $i
by ck2 13y ago
Knowing how to use a language is critical to get expected results.
This gives the proper result in PHP by forcing the integer cast.
$sum = (int) $sum + $i;
- fooyc 13y agoThis casts only $sum to int. If $i is a float, the result is a float. With 32 bits, $sum will be a float bigger than PHP_INT_MAX at some point, and the cast will truncate it, which is unlikely to give the right answer. With 64 bits, which I assume is the platform you tested on, $sum is never bigger than PHP_INT_MAX, so it's never converted to a float, and the (int) cast does nothing (and the computation is done right).
- ck2 13y agoThe result they are showing is clearly on a 64bit machine, my 32bit windows php overflows into a signed integer. I posted initially with casting (int) $i but I tested it without and it worked too. In fact on my 64bit centos machine PHP 5.4 doesn't need the cast at all and the original code gives the correct result the first time as is.
- pc86 13y agoIt's been a long time since I've worked with PHP, I assume $sum += (int) $i; will still convert to float once the size of $sum gets to the requisite size?
- ars 13y agoYes. fooyc explained it correctly. ck2 did not.
- radiospiel 13y agoOne of the reasons to stay away from PHP. Requesting and int but getting a float regardless? That doesn't sit well w/me.
- ngoel36 13y agoCorrection, properly learning about data types and binary representation is critical to "get" programming. I've found that web developers often undervalue (or even scoff at) web developers with CS degrees, but any first-year CS/EECE student at a decent university (or even someone who has taken a couple of Coursera CS classes) would have instantly recognized this to be a floating point related issue. Being able to whip up some scaffolds in Rails is great and immensely useful, but I'm of the opinion that a great Rails programmer will need a proper understanding of the C & Assembly that's behind all the magic.
- bradleyland 13y agoIt's both, really. Knowing the correct data type won't get you anywhere if you use a language like PHP and assume that it will always correctly guess the required data type for a given problem. I find that when it comes to programmers who reach a certain level of proficiency, it doesn't matter whether they learned these things through a CS degree, or through hard knocks. I started out with VBScript and ASP 1.0. I didn't get a CS degree, but I ended up learning about floating point errors rather quickly. This lead me to learn more about data types. Similar result, different path. I ended up paying for my error, much like someone would pay for an education. There are certainly bad programmers without CS degrees who make these mistakes, but I know plenty of programmers who hold a CS degree and will consistently misuse float unless reminded to use something more appropriate.