1.4k times

I have some very simple C++ code:

#include "stdafx.h"
#include "math.h"

int main(int argc, char* argv[])
    long double i,t,c;
    i = 16;
    t = 27;
    c = powl(i, t);

    printf ("%f",  c ); 
    printf ("\n");
    return 0;

According to Microsoft's calculation in Windows, 16^27 is:


According to C++ it is:


Why is this difference in precision happening? It must be completely precise, I am trying to make something simple that will encrypt a message in RSA"

Cipher text = Message **_^ E_** Mod N

If the exponent isn't computed precisely, it will come out incorrect.

Is there anything else that I could define that would be completely precise and hold that large of a number? If someone could direct me towards a class that would do precise math such as this it would be greatly appreciated.

  • 0
    This is a long double (which is the same as double in Win32) and only has about 15 digits of precision. — stalk
  • 0
    "Completely" precise? Of course not. And if you want higher amounts of precision, you'll need a specialized class that implements floating-point arithmetic manually. There are probably some free libraries lying around; maybe GMP includes floating point routines that you can use. — Mas Sehguh
add a comment

1 Answer

  • Votes
  • Oldest
  • Latest

Originally I thought you wanted floating point arithmetic, not integer arithmetic.

In that case, use the GMP Library. You may need to wrap your own class around it, but this library allows you to use math without any limitations. It is a free library that allows you to use precision arithmetic that operates on signed integers, rational numbers, and floating-point numbers.

It actually states that there is no practical limit to the precision, it seems that the main limit might be the available memory on the machine GMP is running on.

Additionally, it states that the main target applications for GMP include cryptography which is exactly what you're trying to achieve here!

add a comment