dfinity / motoko-base

The Motoko base library
Apache License 2.0
483 stars 98 forks source link

Float trapped explicitly: losing precision #302

Open leo-web3 opened 2 years ago

leo-web3 commented 2 years ago

Float.div(Float.fromInt(4999999999999999890753), Float.fromInt(Int.pow(10, 8)));

Call was rejected:
Request ID: 81f4f619948b3a19e47d912a644f8772e60decc32ea9179f622f3cb22c56e735
Reject code: 
Reject text: Canister 3c7jb-myaaa-aaaab-qacoa-cai trapped explicitly: losing precision

image

Float
type Float = Prim.Types.Float
64-bit floating point numbers.

Is float only 64 bit enough for business?

Float trapped explicitly: losing #precision

nomeata commented 2 years ago

This smells like https://github.com/dfinity/motoko-base/issues/272; maybe that fix has not yet reached you

ggreif commented 2 years ago

An older version of Motoko converted Ints to Floats via Int64, which indeed caused problems. Please make sure that you have the most recent dfx.

leo-web3 commented 2 years ago

I used Motoko Playground to test whether Motoko Playground also uses the old version of the SDK

Motoko Playground My Motoko Playground Demo

chenyan-dfinity commented 2 years ago

Motoko playground's default base library is only at dfx 0.8.0, which is the latest tag on motoko-base repo. To get base from master, you can click "Add" button at the "Packages" section and reimport "base". Your example works after the reimport.