lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 163 forks source link

Float negative exponent stores `0` value in the AST #1089

Open Thirumalai-Shaktivel opened 2 years ago

Thirumalai-Shaktivel commented 2 years ago
6.626e-34
$ lpython examples/expr2.py --show-ast --tree --new-parser
└-Module
  |-body=↧
  | └-Expr
  |   └-value=ConstantFloat
  |     |-value=0.000000
  |     └-kind=()
  └-type_ignores=↧
$ lpython examples/expr2.py --show-ast --tree
└-Module
  |-body=↧
  | └-Expr
  |   └-value=ConstantFloat
  |     |-value=0.000000
  |     └-kind=()
  └-type_ignores=↧
akshanshbhatt commented 2 years ago

CPython only takes the first 16 digits after the decimal points, in case of a highly precise decimal value specified by the user.

❯ bat examples/expr2.py
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: examples/expr2.py
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 ~ │ 0.899374917340990314093809431749732947891749873896489364589327
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

❯ python3.11 -m ast examples/expr2.py
Module(
   body=[
      Expr(
         value=Constant(value=0.8993749173409903))],
   type_ignores=[])

Whereas in case of exponential values,

❯ bat examples/expr2.py
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: examples/expr2.py
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1 ~ │ 0.899374917340990314093809431749732947891749873896489364589327e30
   2 ~ │ 1e309
   3 ~ │ 749813274982310948098213094e70
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
❯ python3.11 -m ast examples/expr2.py
Module(
   body=[
      Expr(
         value=Constant(value=8.993749173409903e+29)),
      Expr(
         value=Constant(value=inf)),
      Expr(
         value=Constant(value=7.498132749823109e+96))],
   type_ignores=[])