0

I am trying to find the scientific notation with double and i could not find any function or way to do this.

My variable is double and I need to get the scientific notation from it, for example:

3.44828e+026

I need to somehow get the number 026 from my double variable.

By the way, the number is actually 28 long and not 26, is there a way to fix that too? (not adding 2 to the result)

Thanks in advance!

Naor Hadar
  • 537
  • 1
  • 6
  • 19
  • 3
    You're looking for the *logarithm*... – Kerrek SB Oct 10 '13 at 08:13
  • 1
    If the wrong exponent is displaying, you must be dividing the number by 100 before printing it. Computers don't usually get simple arithmetic wrong like that. – Barmar Oct 10 '13 at 08:14
  • actually I am not dividing by 100. – Naor Hadar Oct 10 '13 at 08:15
  • possible duplicate of [How to count amount of digits in a given number in c++](http://stackoverflow.com/questions/4624490/how-to-count-amount-of-digits-in-a-given-number-in-c) – Barmar Oct 10 '13 at 08:16
  • @NaorHadar Show us your code then. – zakinster Oct 10 '13 at 08:17
  • double num = ((pow(10.0,28.0)-1.0)/29.0); im printing num. – Naor Hadar Oct 10 '13 at 08:19
  • @NaorHadar `1e+28 - 1 = 9.99e+27`, if you divide it by `29` it will certainly be `3.xx e+26`. Why are you expecting `e+28` ? – zakinster Oct 10 '13 at 08:22
  • 1
    @zakinster `1e+28 - 1` is `1e28` on most machines. (Machine floating point values are _not_ real numbers, and don't obey the rules of real number arithmetic.) – James Kanze Oct 10 '13 at 08:26
  • 1
    @JamesKanze Good point, but on those machine, `1e+28 == 9.99e+27`, anyway that doesn't change the result, `1e+28 / 29 = 3.xx e+26`. – zakinster Oct 10 '13 at 08:33
  • @zakinster Not a all. On my Windows machine, for example, 1E+28 is actually 9.9999999999999996e+027, while 9.99E27 is 9.990000000000001e+027. If I dump them, they have different binary representations. – James Kanze Oct 10 '13 at 09:31
  • @JamesKanze I didn't put all the `9` for the sake of readability, but of course I surely hope there's no system in which `9.99` actually equals `10` (the exponent not begin part of the significant precision). When I wrote `9.99e+27`, I actually meant `9.9999999...e+27`. – zakinster Oct 10 '13 at 09:36
  • @zakinster A couple of dots after the 9.99 would have made that clearer. I agree that lots of 9's don't help readability. And the exact number you'd need will depend on the architecture. (Also, the standard guarantees at least 10 digits for a `double`, so an implementation where `1e28` and 9.99e27` were equal wouldn't be conform.) – James Kanze Oct 10 '13 at 09:40

2 Answers2

1

This should work if y is non-negative. For negative y, the result if off-by-one. Fix it yourself if needed.

#include <iostream>
#include <cmath>

int main()
{
    double x = 3.44828e+026;
    int y = (int)log10(x);
    std::cout << y << std::endl;  
    return 0;
}

Output: 26

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

Answer similar to Yu Hao's but this one seems to work for negative exponents as well (but I haven't fully tested it):

#include <iostream>
#include <cmath>

// pre-condition: x != 0.0
int exponent(double x) {
    double y = log10(x);
    if (y < 0.0)
        y -= 1.0;
    return static_cast<int>(y);

}

int main() {
    std::cout << exponent(3.44828e+026) << '\n';
    std::cout << exponent(3.44828e-026) << '\n';
}

Outputs 26 and -26.

Cassio Neri
  • 19,583
  • 7
  • 46
  • 68