Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

99
Visualizações
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
1 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda