Home > Blockchain >  Why pow() function in python and pow() function in dart produces different result for same input?
Why pow() function in python and pow() function in dart produces different result for same input?

Time:01-07

I want to evaluate pow(17,18) in Dart language, but it produces 6665467901046659617. In Python language the same expression pow(17,18) produces 14063084452067724991009.

I want to achieve the same result in Dart also.

CodePudding user response:

It looks like Dart runs into an overflow at some point and does not (like Python) support arbitrarily large integers.

You are likely using Dart 2, which has a limit of 64 bits for integers. The maxint for that size is 9,223,372,036,854,775,807. Dart 1 did have a similar integer math implementation to Python, allowing integers of arbitrary size (limited by available memory only).

So, it makes sense that you're seeing a value like 6,665,467,901,046,659,617, which is just the result of pow(17,18) modulo the maximum integer value for Dart 2 1.

However, Dart 2 still has BigInt, exactly for this purpose. As user @mmcdon20 indicated in the comments, you're looking for:

BigInt.from(17).pow(18)
  • Related