minimum float value

M

Mark P

I have a float value which should be initialized to the minimum allowed
value (analogous to -infinity). I discovered today (thank you, unit
tests!) that numeric_limits<float>::min() returns a very small number
greater than 0, rather than the extremely negative number which I want.
Is it safe to use -numeric_limits<float>::max() for this purpose, or
is something else preferred?


Thanks,
Mark
 
G

Greg Buchholz

Mark said:
I have a float value which should be initialized to the minimum allowed
value (analogous to -infinity).

What about actually using -infinity?

float init = numeric_limits<float>::has_infinity
? -numeric_limits<float>::infinity()
: -numeric_limits said:
I discovered today (thank you, unit
tests!) that numeric_limits<float>::min() returns a very small number
greater than 0, rather than the extremely negative number which I want.
Is it safe to use -numeric_limits<float>::max() for this purpose

In general? I suppose no. In practice, if your "float" represents
an IEEE754/854 number (a very common floating point standard), then you
should be safe, since a sign bit stored separately (no two's
complement).

Greg Buchholz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top