Skip to content

Commit

Permalink
Provide toReal for Moscow ML Arb{int,num}
Browse files Browse the repository at this point in the history
  • Loading branch information
mn200 committed Oct 27, 2024
1 parent 3ec6fe2 commit f7bb5bb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/portableML/mosml/Arbintcore.sig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ sig
val fromNat : num -> int
val toInt : int -> Int.int
val toNat : int -> num
val toReal : int -> real

val + : (int * int) -> int
val - : (int * int) -> int
Expand Down
3 changes: 3 additions & 0 deletions src/portableML/mosml/Arbintcore.sml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ fun fromInt n =
if (Int.<(n,0)) then (false, Arbnumcore.fromInt(Int.-(0, n)))
else (true, Arbnumcore.fromInt n)

fun toReal (true,n) = Arbnumcore.toReal n
| toReal (false,n) = Real.~(Arbnumcore.toReal n)


fun min (i,j) = if i < j then i else j
fun max (i,j) = if i < j then j else i
Expand Down
2 changes: 1 addition & 1 deletion src/portableML/mosml/Arbnumcore.sig
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ sig

val fromInt : int -> num (* raises Overflow if i < 0 *)
val toInt : num -> int (* raises Overflow if n > maxInt *)
val toReal : num -> real (* raises Overflow if real calcs do so; no posinf for you! *)

val floor : real -> num (* raises Overflow if r < 0 *)
val toReal : num -> real

val asList : num -> int list

Expand Down
8 changes: 8 additions & 0 deletions src/portableML/mosml/Arbnumcore.sml
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,14 @@ in
val toHexString = toBaseString (fromInt 16)
end

fun toReal0 f A is =
case is of
[] => A
| i :: rest => toReal0 (Real.*(f,Real.fromInt BASE))
(Real.+(A,Real.*(f,Real.fromInt i)))
rest
fun toReal an = toReal0 1.0 0.0 an

fun isqrt n =
if n < two
then n
Expand Down

0 comments on commit f7bb5bb

Please sign in to comment.