2

What is a platform-independent way of specifying the largest representable negative floating-point number?

We found an algorithm that broke when run on a PS3's SPU, but worked fine when compiled for the PPU:

float x = -FLT_MAX;
/* stuff */
if (x > 0.0f) {
    // If x is unchanged, code is executed on SPU
}

Essentially, is there a well-defined negative equivalent of FLT_MAX?

Blair Holloway
  • 15,969
  • 2
  • 29
  • 28
  • Why not use negative infinity? –  Sep 03 '10 at 05:46
  • Unless the SPU and the PPU have different float precisions, this should work. I suspect there's a problem in the commented out /*stuff*/ section of your code. – Drew Hall Sep 03 '10 at 06:21
  • It's unclear (to me, anyway) what you expect the code to do and what it actually does. And given how floating point numbers are represented, isn't the negative equivalent of `FLT_MAX` just `-FLT_MAX` (which you're already using)? Exactly what's not working? – jamesdlin Sep 03 '10 at 06:38
  • possible duplicate of [Obtain minimum NEGATIVE float value in C++](http://stackoverflow.com/questions/3529394/obtain-minimum-negative-float-value-in-c) – sbi Sep 03 '10 at 07:44
  • Why does the code think that `-FLT_MAX` is > 0.0f ? That does not seem to make sense to me! – Johannes Schaub - litb Sep 03 '10 at 15:58
  • 2
    @stacker - `FLT_MIN != -FLT_MAX` – Blair Holloway Sep 06 '10 at 07:10
  • @Blair yes,I expected something like FLT_MIN/MAX 1.17549435e-38f (negative) != 3.40282347e+38f (positive) – stacker Sep 06 '10 at 07:50
  • Check [here](http://stackoverflow.com/questions/3529394/obtain-minimum-negative-float-value-in-c/3529429#3529429). *May* be useful – Chubsdad Sep 03 '10 at 05:45

2 Answers2

6

You want std::numeric_limits::lowest(), but it is c++0x only, so not very cross platform at the moment.

You definitely don't want std::numeric_limits::min() - that is smallest magnitude, not furthest negative.

If you want something that will always be less than all other doubles, use -numeric_limits<double>::infinity().

njamesp
  • 760
  • 5
  • 11
4

Without knowing what's in /* stuff */, I don't think your problem can be fully addressed here.

There's a good set of slides on the problems inherent in floating point calculation here: http://realtimecollisiondetection.net/pubs/GDC07_Ericson_Physics_Tutorial_Numerical_Robustness.ppt - there may be some hint for you in there as to the source of your problem.

IEEE 754 single precision floating point is not the same on the SPU as it is on the PPU - there's a full explanation in chapter 9 of the SPU ISA document available from http://cell.scei.co.jp/e_download.html which also includes the maximum magnitude of a single precision floating point number.

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
Jonathan
  • 76
  • 2
  • I'm accepting this as the answer -- after reading the linked document detailing the SPU architecture, it became apparent that `-FLT_MAX` wasn't going to work. – Blair Holloway Sep 06 '10 at 07:09
  • As an aside, the code in `/* stuff */` shouldn't matter, as `x` was only touched in certain conditions that weren't being met; `x` was unchanged by the time we check `x > 0.0f`, which was why there was much head-scratching when it passed! – Blair Holloway Sep 06 '10 at 07:10
  • Oh, I missed the comment inside the if block. Happy to hear that you've worked it out :) – Jonathan Sep 08 '10 at 13:58