Maybe it's a dumb question, I don't know…

Why "Math.Truncate()" (trunc() in C++) returns a double?

Its only purpose is to return the integer part of a double, which is a fucking integer…

Same for floor() and ceil().

My point is that you can put an int in a double without any problem (so they could have return an int), not the other way around, so you have to convert it if you need it in an int.

  • 4
    Because max int is 2147483647, while trunc / floor / ceil of a double can have up to 308 digits.
  • 0
    @hitko Ok, that makes sense, thanks!
  • 2
    BUT... it was NOT a dumb question.
  • 2
    Also NaN is a valid float value but can't be represented by an int. floor(NaN) == NaN
  • 1
    @momad Another valid reason, thanks!
    I didn't think about a lot of cases, my bad.

    @devnulli thanks, but it was. 😅
    I only thought about my personal situation, not the many other cases possible.

    The "return the integer part of a double" didn't make my think about the fact it doesn't mean a simple integer, because as others said here it can be much bigger.
Add Comment