5 ms·
Would need some imports: import cmath from fractions import Fraction And then have to write numbers as strings to input them. From their examples:
by zelphirkalt 24d ago
Would need some imports:
import cmath
from fractions import Fraction
And then have to write numbers as strings to input them. From their examples:
>>> Fraction('1.414213 \t\n')
Fraction(1414213, 1000000)
>>> cmath.sqrt(-2-0j)
-1.4142135623730951j
Not really great, but workable. I don't like having to import things for basic math stuff like exact numbers, but having a lot of special syntax is maybe also not that great. I am guessing except for maybe macros, there is no other way, if one wants to take input (code) literally as exact numbers in all cases. If one were to just write down a number like `1.414213562373`, and expect it to be exactly represented in the program, then it cannot be read as a floating point number first and then converted into an exact number internally, because reading it as floating point number could already lose precision. So this kind of logic to interpret exact numbers would have to live somewhere before that. So it is in the reader. I am not sure how one could employ a macro instead, and then maybe access the digits of a written number, or whether that is possible. I think it should be possible. If it is, then of course things could be made into macros, after which one could use something like:
(exact 1.4142135623730951)
[1]: https://docs.python.org/3/library/cmath.html https://docs.python.org/3/library/cmath.html
[2]: https://docs.python.org/3/library/fractions.html https://docs.python.org/3/library/fractions.html
- nilslindemann 24d ago[dead]