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

151
Views
¿Por qué recibo un desbordamiento del sistema usando un entero grande (System.Numerics)?

Recibo este error: System.OverflowException: 'El valor no es un número'. Tenía la impresión de que un entero grande podría almacenar cualquier valor de tamaño (500 ^ 500 en este caso), así que no entiendo por qué sucede esto.

 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 answers
Answer question

0

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

Bueno, Math.Pow(code, d) % pq no es un BigInteger, es una expresión de tipo double . La conversión del resultado a BigInteger no tendrá efecto hasta que se complete el cálculo (y se haya desbordado).

Math.Pow puede desbordarse fácilmente a Double.PositiveInfinity con números grandes, y Double.PositiveInfinity % someNumber produce Double.NaN . Llamar new BigInteger(Double.NaN) produce el error que ha descrito.

Necesita hacer el cálculo en BigInteger. Afortunadamente, hay un método exactamente para ese propósito ( BigInteger.ModPow ):

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

(BigInteger.ModPow requiere parámetros BigInteger, pero hay conversiones implícitas de int a BigInteger).

over 4 years ago · Santiago Trujillo Report

0

Parece que pq es 0 , luego lanza la misma excepción:

 new BigInteger(0.0 % 0);

La división de double por cero da como resultado Double.NaN que no es un valor válido para instanciar un BigInteger de acuerdo con docs :

Excepciones

OverflowException

el value es NaN, NegativeInfinity o PositiveInfinity.

O, como @Heinzi mencionó correctamente en los comentarios, Math.Pow da como resultado Infinity.

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!