views:

36

answers:

1

What's the least (negative) integer value that can be exactly represented by Double type in all major x86 systems? Especially in (simultaneously) JVM, MySQL, MS SQL Server, .Net, PHP, Python and JavaScript (whatever corresponding type it uses).

The reason why I ask about this is because I'd like to choose a value to use to represent an error (to return from a function in case it couldn't be calculated successfully) - that's why I need an exact value to be 100% predictable for exact equality checks. Exceptions (try-catch) seem to work much slower - that's why I need such a thing.

A: 

Most of your languages and database products will use IEEE Standard 754 singles and doubles. In C and C++, you can use single extended formats and double extended formats, but these are architecture dependent formats. Your high-level, platform neutral languages and databases probably avoid exposing these.

According to IEEE Standard 754, you ignore that a double can store Infinity and -Infinity and NaN then:

  • max normal number: 1.7976931348623157e+308
  • min positive normal number: 2.2250738585072014e-308
  • max subnormal number: 2.2250738585072009e-308
  • min positive subnormal number: 4.9406564584124654e-324

For x86 double extended format:

  • max normal 1.18973149535723176505e+4932
  • min positive normal: 3.36210314311209350626e-4932
  • max subnormal: 3.36210314311209350608e-4932
  • min positive subnormal: 3.64519953188247460253e-4951

It works out to be a double has:

  • Significant Digits (Binary): 53
  • Smallest Positive Normal Number: 2.225... 10-308
  • Largest Positive Number 1.797... 10308
  • Significant Digits (Decimal): 15-17

And a double extended (x86) has:

  • Significant Digits (Binary): 64
  • Smallest Positive Normal Number: 3.362... 10-4932
  • Largest Positive Number 1.189... 104932
  • Significant Digits (Decimal): 18-21
Zac Bowling
I think values of such big magnitudes are not represented exactly. The reason why I ask about this is because I'd like to choose a value to use to represent an error (to return from a function in case it couldn't be calculated successfully) - that's why I need an exact value to be 100% predictable for exact equality checks.
Ivan
Why not `NaN` ?
Adam Goode
@adam-goode, This was first to come into my mind, I've tried. I can't remember exactly how it was, but it was refused by Scala (the main language I use) compiler.
Ivan
I would normally point out that "YOUR DOING IT WRONG" using a unqiue value in a double to represent something special. :-) Although back when I wrote a lot of C, I would return -1 to signify not found when looking up an ID. I love my structured programming languages these days, where I can just throw an exception or just return a tuple or a out param or a structure/class that contains the value and the state code.
Zac Bowling
@zac-bowling, I understand, this seems a dirty hack for me to. But exceptions seem to work much slower and I really need lightning-fast error handling and can't afford any significant time overhead (because 1. this is a number-crunching task, 2. such functions denial occur frequently and usually come in series).
Ivan
@zac-bowling, at ze same time, using a tuple instead of a simple Double and having a dedicated fiekd just to indicate errors seems even dirtier, imho. It'd be a violation of "Occam's razor".
Ivan
@zac-bowling, currently I use -101, but 'd like to change this as this value is an improbable (-1 is probable) to occur naturally in my task, but still makes sense having a physical meaning there. While values of higher magnitudes (like -10000000001) are very improbable and I could sacrifice one easily.
Ivan
negative infinity then maybe?
Zac Bowling
can I get an accept?
Zac Bowling