7

I don't have any good reference about std::numeric_limits<T>, but sites on the net says that std::numeric_limits<T>::epsilon() will return the difference between 1 and the smallest number after it. As far as I know about the int type, the next numer after 1 is 2, so epsilon should be 1. But it's 0 (Linux, g++ 4.4.5). What is the reasoning behind that?

I know that in practice epsilon() is only useful for floating point types, I'm just nitpicking.

2 Answers 2

8

The standard says ([numeric.limits.members])

Meaningful for all floating point types.

The standard also says ([numeric.special])

many values are only required to be meaningful under certain conditions (for example, epsilon() is only meaningful if is_integer is false). Any value that is not “meaningful” shall be set to 0 or false.

Since the value is not "meaningful", the value 0 is required by the standard as an indication that it is not a meaningful value.

Sign up to request clarification or add additional context in comments.

2 Comments

+1, but I wouldn't say that zero value is an indication of it being not meaningful :)
@unkulunkulu I mean that a value of 1 could hypothetically be a floating point type with horrible precision, but a value of 0 could never possibly match the description of epsilon()
2

I'd say it returns the range for which numbers are considered to be equal, with integer types that range ought to be 0.

Ie if you're creating generic functions which can handle both integer and floating point types, you'd like the range to be 0 for integers.

1 Comment

I like the idea, even if it's not the real case :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.