4 ms·
> Anything that has one right answer is going to smell like a made-up classroom assignment. If you're doing C (or any other language where that particular prob
by majewsky 10y ago
> Anything that has one right answer is going to smell like a made-up classroom assignment.
If you're doing C (or any other language where that particular problem exists), it's always a nice exercise to have students write a program (or a function) that takes the radius of a circle and outputs its volume. Most students will write:
double volume(double radius) {
return 4 / 3 * M_PI * radius * radius * radius;
}
Can you see the problem? ;)
- the-dude 10y agoCircle has no volume
- noxToken 10y agoAlthough I laughed, I'm sure GP meant sphere. That is the correct formula for volume of a sphere, and a sphere is a circle in 3-space.
- the-dude 10y agoSure. But getting the spec right is more important than being smug about a silly mistake only novices make. And C has much more interesting caveats than this one.
- majewsky 10y agoAw snap. Yeah, of course I meant a sphere. (And technically, it's the other way around. A circle is the 2-dimensional special case of a (hyper)sphere.)
- julian_1 10y ago4 / 3 would be computed as an integer (eg. 1), before the result is implicitly coerced to float for the rest of the expression calculation? A nice example for debugging, and for understanding the value of languages with better typing discipline.