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

152
Visualizações
Why am I getting a system overflow using big integer (System.Numerics)?

I am getting this error: System.OverflowException: 'The value is not a number.' I was under the impression that big integer could store any sized value (500 ^ 500 in this case), so I don't understand why this is happening.

public int decode(int code)
{
   int totient = (p - 1) * (q - 1);
   int d = modInverse(e, totient);
   int pq = p * q;
   BigInteger decodedMessage = new BigInteger(Math.Pow(code, d) % pq);
   return (int)decodedMessage;
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

BigInteger decodedMessage = new BigInteger(Math.Pow(code, d) % pq);

Well, Math.Pow(code, d) % pq is not a BigInteger, it's an expression of type double. Converting the result to a BigInteger won't have an effect until the computation is complete (and has overflowed).

Math.Pow can easily overflow to Double.PositiveInfinity with large numbers, and Double.PositiveInfinity % someNumber yields Double.NaN. Calling new BigInteger(Double.NaN) yields the error you have described.

You need to do the computation in BigInteger. Fortunately, there's a method for exactly that purpose (BigInteger.ModPow):

BigInteger decodedMessage = BigInteger.ModPow(code, d, pq);

(BigInteger.ModPow requires BigInteger parameters, but there are implicit conversions from int to BigInteger.)

over 4 years ago · Santiago Trujillo Relatório

0

It seems that pq is 0, next throws the same exception:

new BigInteger(0.0 % 0);

Division of double by zero results in Double.NaN which is not a valid value to instantiate a BigInteger according to docs:

Exceptions

OverflowException

value is NaN, NegativeInfinity, or PositiveInfinity.

Or, as @Heinzi correctly mentioned in comments Math.Pow results in Infinity.

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