[phobos] phobos commit, revision 1833

Jonathan M Davis jmdavisprog at gmail.com
Thu Aug 12 00:39:40 PDT 2010


On Wednesday 11 August 2010 23:40:17 Daniel Murphy wrote:
> Do we really want the sqrt of a integer to return a real?
> I would expect it to return floor(sqrt(n)), in the same way that integer
> division does.
> What do other people think/expect?


That seems rather limiting. Don't you usually want a floating point value from 
sqrt? If you want an int, you can just cast the result. Otherwise, you're going 
to have to cast the ints that you pass to sqrt() in order to get a floating value 
result, which would tend to be even more annoying - particularly since I would 
expect the programmer to want a floating point result normally anyway. On top of 
that, because an int value can be held just fine in a floating point, making 
sqrt() return an int could easily cause bugs where someone expects it to return 
a floating value and assigns it to a floating value. The assignment works just 
fine, but they never get the result that they expect, and they may have a hard 
time tracking down such a bug.

If it returns a real, then it does what I would expect most people to want, and 
if you want an int, you have to cast it. It's nice and clear, and you don't have 
errors due to something being a type that you don't expect.

- Jonathan MDavis


More information about the phobos mailing list