문제

I'm currently trying to make a simple calculator, but I want it to use somewhat big numbers(anything above 10^10 should do). And since any decent calculator has floating point operations, I decided to use double as my type. Sadly, when I tried to write a big number(around 10^7), it went into e notation. I want to display it like a normal number.

All help is appreciated. :D

도움이 되었습니까?

해결책

If you want to display all digits of your double (instead of going into scientific notation), you have to change your output stream's float formatting to std::fixed:

Live demo on Coliru:

double d = 1000000000000000.0; // 10^16
std::cout << std::fixed << d;

Output:

1000000000000000.000000

If you want your display to go into scientific notation at a custom exponent, you'll have to do that yourself using the existing tools.

다른 팁

You could try out GMP library for for arbitrary precision arithmetic. If you're using it on Windows you can neatly install it using mingw-get.

For 64bit double you could use long double, I think. I have personally never needed this.

And also there are processor extensions for longer floating point registers which I don't know much about and take a bit of effort to use for trivial tasks.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top