Does casting to an int after std::floor guarantee the right result?

前端 未结 5 1492
旧巷少年郎
旧巷少年郎 2020-11-30 05:38

I\'d like a floor function with the syntax

int floor(double x);

but std::floor returns a double. Is<

5条回答
  •  日久生厌
    2020-11-30 05:54

    Most of the standard math library uses doubles but provides float versions as well. std::floorf() is the single precision version of std::floor() if you'd prefer not to use doubles.

    Edit: I've removed part of my previous answer. I had stated that the floor was redundant when casting to int, but I forgot that this is only true for positive floating point numbers.

提交回复
热议问题