Skip to content

Commit

Permalink
fix slowness for large exponents
Browse files Browse the repository at this point in the history
  • Loading branch information
winitzki committed Oct 15, 2024
1 parent 0b211cc commit f8eada2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
16 changes: 16 additions & 0 deletions tutorial/Float/Type.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ let Float/normalize
= float2float_reduce
(λ(x : Float) Float/addExtraData (FloatBare/normalize x.(FloatBare)))

let _
-- Should not be slow even if the exponent is large.
=
assert
: FloatBare/normalize
(FloatBare/create +1 +1000000000000000000000000000000000000)
FloatBare/create +1 +1000000000000000000000000000000000000

let Float/pad
: Float Natural Float
= stop.reduce_growth
Expand Down Expand Up @@ -265,6 +273,14 @@ let _ =
: (Float/pad (Float/create +123 +0) 2).(FloatBare)
FloatBare/create +12300 -2

let _
-- Should not be slow even if the exponent is large.
=
assert
: Float/normalize
(Float/create +1 +1000000000000000000000000000000000000)
Float/create +1 +1000000000000000000000000000000000000

let Float/negate =
float2float_reduce
( λ(a : Float)
Expand Down
2 changes: 1 addition & 1 deletion tutorial/Float/divide.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ let _
let _
-- Should not be slow even when exponents are very large.
=
let power = +100
let power = +1000000000000000000000000000000000000

in assert
: check
Expand Down
31 changes: 20 additions & 11 deletions tutorial/Float/show.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,11 @@ let _ = assert : Natural/lessThan 1 Base ≡ True

let _ = assert : Natural/lessThan 1 Digits True

let MaxBase = T.power Base Digits

let Text/repeat =
λ(n : Natural)
λ(x : Text)
Natural/fold n Text (λ(a : Text) a ++ x) ""

let maxDisplayedInteger = T.power Base (MaxPrintedWithoutExponent + 1)

let padRemainingDigits
: { digits : Natural, length : Natural } Text
= λ(args : { digits : Natural, length : Natural })
Expand Down Expand Up @@ -155,15 +151,21 @@ let Float/showNormalized

let rest =
if Integer/nonNegative f.exponent
then let largeInteger =
f.mantissa
* T.power Base (Integer/abs f.exponent)
then let largeIntegerLog =
f.topPower + Integer/clamp f.exponent

in if Natural/lessThan
largeInteger
maxDisplayedInteger
in if Natural/lessThanEqual
largeIntegerLog
MaxPrintedWithoutExponent
then Text/concat
[ Natural/show largeInteger, "." ]
[ Natural/show
( T.power
Base
(Integer/clamp f.exponent)
* f.mantissa
)
, "."
]
else `number is above 1000 with positive exponent, so print as 1.234...e+...`
f
else if Natural/lessThan
Expand Down Expand Up @@ -320,4 +322,11 @@ let _ = assert : test_show +110000 -1 ≡ "+1.1e+4"

let _ = assert : test_show -110000 -1 "-1.1e+4"

let _
-- Should not be slow even if the exponent is large.
=
assert
: test_show +1 +1000000000000000000000000000000000000
"+1.e+1000000000000000000000000000000000000"

in Float/show
5 changes: 3 additions & 2 deletions tutorial/Functor.dhall
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- Definition of the Functor typeclass.
let Fmap_t = λ(F : Type Type) (a : Type) (b : Type) (a b) F a F b
let Fmap_t =
λ(F : Type Type) (a : Type) (b : Type) (a b) F a F b

let Functor = λ(F : Type Type) { fmap : Fmap_t F }

in { Fmap_t, Functor }
in { Fmap_t, Functor }

0 comments on commit f8eada2

Please sign in to comment.