3

Test case :

35000

-> a normalized scientific notation of the number would be 3.5 * 10E4

-> the engineering notation would be 35 * 10E3

A simple algorithm that does this would keep dividing the number by 10 until we get the notations required. However this would mean the algorithm would be O(number of zeros). Can we do better?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
unj2
  • 52,135
  • 87
  • 247
  • 375
  • If it's a double you can extract the mantissa directly to get a very good first approximation, though this would break at the extreme ends where doubles are no longer stored normalized. – Roman Starkov Jan 29 '12 at 17:11
  • 1
    Assuming a fast implementation of log, int exp = floor(log(x)) and expEng = 3 (exp / 3) – Adam Liss Jan 29 '12 at 17:34
  • @ Adam, The question is how efficient is the log? – unj2 Jan 29 '12 at 17:49

1 Answers1

7

The classic paper on the subject of printing human-friendly representations of floating point numbers can be read here. It's much too complex to be discussed as code in an answer here.

bmargulies
  • 97,814
  • 39
  • 186
  • 310