18

I am assigned some old code and when I was reading through it, I noticed it had these in the form of:

float low = 1e-9;
float high = 1e9;

float lowB = 1e-9;
float highB = 1e9;

float lowL = 1e-9;
float highL = 1e9;

So I see that it's trying to define some ranges using the e notation, right? But isn't 1e-9 supposed to be -1e9?

Then the values would be between -1000000000 and 1000000000, right?

I am not sure what 1e-9 is meant for?

Joan Venge
  • 269,545
  • 201
  • 440
  • 653
  • The code as written is valid. It's not possible to say whether it does what it's intended to do from looking at it. – Pete Becker Aug 26 '12 at 22:21
  • Thanks, it works as of now, so I won't touch it. I will just maintain it for a while. – Joan Venge Aug 26 '12 at 22:22
  • Without context it's impossible to tell whether it makes sense for the low values to be really big negative numbers or really small positive numbers. If the range is zero to whatever, then certainly negative numbers don't make sense. – tripleee Aug 26 '12 at 22:22
  • Also on a side note, anyone knows how far can you go and be safe in the float range? I see `1e16`, but that was the largest I have seen. – Joan Venge Aug 26 '12 at 22:25
  • 1
    The first rule for maintaining code is if you don't understand what it's doing, don't change it. `` – Pete Becker Aug 26 '12 at 22:25
  • Why are people downvoting it? It's a perfectly acceptable question. – Joan Venge Aug 26 '12 at 22:28
  • 1
    All floating-point types are required to support decimal exponents from -37 to 37. – Pete Becker Aug 26 '12 at 22:29
  • 1e-9 is nano scale. as in nanoseconds in a second. 1e9 is the opposite direction. – uchuugaka Jul 26 '13 at 14:34

2 Answers2

69

Neither is more correct than the other. They just represent different values.

1e-9 is 0.000000001; the minus sign applies to the exponent.

-1e9 is -1000000000.0; the minus sign applies to the number itself.

The e (or E) means "times 10-to-the", so 1e9 is "one times ten to the ninth power", and 1e-9 means "one times ten to the negative ninth power". In mathematical scientific notation, this is usually denoted by a superscript: 1 × 10-9 or -1 × 109. Programming languages adopted the e or E notation because it was easier to type and print than a superscript (and still is, for that matter). (I think this may have been introduced by Fortran in the 1950s, but I'm not sure of the exact history.)

Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
4

"Low" and "high" refer to absolute values. The low number is small in absolute values, the high one is large. Negatives aren't important, since you already understand how to work with those. What's important about floats is their variable scale (i.e. the exponent), and so it is customary to provide lower and upper bounds for the scale rather than the value.

Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025