Modulus and Porting C# to C++

Hi Guys,

I'm porting a Windows C# application to C++ on 64-bit Ubuntu v14.04. The problem I'm having is that the c# version uses a very large decimal #, 7317798979557510901670260274, and performs a modulus on it.

The c# code:
1
2
  decimal num = 7317798979557510901670260274;
  int value = (num % 26);


In C#, value = 6.

The c++ code:
1
2
  long double num = 7317798979557510901670260274.0;
  int value = (int)std::fmod(num, 26);

In C++, value = 12.

Can anyone tell me what I'm doing wrong and point me to a solution?

Thanks very much.....
Last edited on
Use an int64_t instead of long double.
The number being used is too large for an int64_t.

main.cpp:59:23: warning: integer constant is too large for its type [enabled by default]
In that case, you need to use an arbitrary precision library such as GMP. https://gmplib.org/

Edit: You can also use std::decimal128 with a recent GCC and -std=gnu++11
Last edited on
Thanks very much. The GMP library solved my problem.
Topic archived. No new replies allowed.