Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
Different results between c++ and c# sin function with large values

I came across such strange behavior of the Math.Sin function in C#, when I use large numbers; for example:

C#: .Net 4.7.2: Math.Sin(6.2831853071795856E+45) = 6.2831853071795856E+45

C++: sin(6.2831853071795856E+45) = -0.089650623841643268

Any ideas how to get the same results as C++?

C# sample:

double value = 6.2831853071795856E+45;    
Console.WriteLine(Math.Sin(value));

C++ sample:

double x = 6.2831853071795856E+45;
double result;
    
result = sin(x);
cout << "sin(x) = " << result << endl;
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The sin of large numbers like 6.2831853071795856E+45; makes no sense. Remember that Pi is approximately 3.14 and that your floating numbers are probably IEEE 754 (see the http://floating-point-gui.de/ for more)

The computed number is probably entirely wrong.

Consider using static analysis tools like Fluctuat on your C or C++ code. Or dynamic tools like CADNA

As a rule of thumb, trigonometric functions like sin, cos, tan should not be used with large numbers (e.g. more than a few thousands times PI ℼ = 3.14 in absolute value).

Otherwise, use a bignum library like GMPlib. It will slow down calculations by a factor of thousands, but it could give you meaningful results for sin (pi * 1.e+45)

And please read something about Taylor series (they are related to trigonometric functions)

Try also to use GNUplot to "visualize" the sin function (for reasonable numbers).

Mathematically the sin of every finite real number is between -1 and 1. So Math.Sin(6.2831853071795856E+45) = 6.2831853071795856E+45 is wrong.

Read also conference papers like this one and that one.

over 4 years ago · Santiago Trujillo Report

0

f(x) = sin(x) oscillates between plus and minus 1 with period 2 * pi, i.e. sin(x) = sin(N * 2 * pi + x).

Forget about programming language and forget about sin() for a moment and think about what the value in scientific notation 1.23E+45 means. It could represent any value from 1225000...(41 zeros) to 1234999...(42 nines), i.e. 1E43 possible integers (or an infinite number of values if fractions are allowed).

I'm playing fast and loose with strict definitions here by speaking in decimal terms and equating significant digits with precision. Find links here to learn more: https://en.wikipedia.org/wiki/Precision. I also hope I've got my exponent arithmetic right, but the implication should be clear regardless.

Back to sin(), C# and C++: since the pure mathematical function output repeats every 2 * pi (~6.28) and double precision floats (IEEE 754 64 bit doubles https://en.wikipedia.org/wiki/IEEE_754) in both C# and C++ have only 15-17 significant decimal digits, no output from the sin() function can possibly be meaningful because the input values are "missing" (imprecise by) at least 30 significant/relevant decimal digits.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!