Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/Fable.Transforms/Python/Fable2Python.Transforms.fs
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,6 @@ let transformValue (com: IPythonCompiler) (ctx: Context) r value : Expression *
| Fable.NumberValue.Float64 x when x = -infinity ->
Expression.unaryOp (UnaryMinus, com.GetImportExpr(ctx, "math", "inf")), []
| Fable.NumberValue.Float64 x when Double.IsNaN(x) -> com.GetImportExpr(ctx, "math", "nan"), []
| Fable.NumberValue.Float32 x when Single.IsNaN(x) ->
libCall com ctx r "core" "float32" [ Expression.stringConstant "nan" ], []
| Fable.NumberValue.Float16 x when Single.IsNaN(x) ->
libCall com ctx r "core" "float32" [ Expression.stringConstant "nan" ], []
| Fable.NumberValue.Float16 x -> makeFloat com ctx r value.Type "float32" (float x)
| Fable.NumberValue.Float32 x -> makeFloat com ctx r value.Type "float32" (float x)
| Fable.NumberValue.Float64 x -> Expression.floatConstant (float x, ?loc = r), []
Expand Down
12 changes: 9 additions & 3 deletions src/Fable.Transforms/Python/Fable2Python.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,10 +1037,16 @@ module Util =
| _, (:? uint64 as i) when i = 64UL -> makeFieldGet cons "SIXTY_FOUR"
| _ -> Expression.call (cons, [ value ], ?loc = r), []

let makeFloat (com: IPythonCompiler) (ctx: Context) r _t floatName x =
let makeFloat (com: IPythonCompiler) (ctx: Context) r _t floatName (x: float) =
let cons = libValue com ctx "core" floatName
let value = Expression.floatConstant (x, ?loc = r)
Expression.call (cons, [ value ], ?loc = r), []

match x with
| x when Double.IsPositiveInfinity x -> makeFieldGet cons "infinity"
| x when Double.IsNegativeInfinity x -> makeFieldGet cons "negative_infinity"
| x when Double.IsNaN x -> makeFieldGet cons "nan"
| _ ->
let value = Expression.floatConstant (x, ?loc = r)
Expression.call (cons, [ value ], ?loc = r), []

// ---------------------------------------------------------------------------
// Int32 normalization
Expand Down
8 changes: 8 additions & 0 deletions src/Fable.Transforms/Python/PythonPrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,14 @@ module PrinterExtensions =
printer.Print("\"")
printer.Print(Naming.escapeString (fun _ -> false) value)
printer.Print("\"")
| Constant(value = FloatLiteral value) when Double.IsNaN value || Double.IsInfinity value ->
// .NET prints these as `Infinity`, `-Infinity` and `NaN`, which are not Python names
if Double.IsNaN value then
printer.Print("float(\"nan\")")
elif Double.IsPositiveInfinity value then
printer.Print("float(\"inf\")")
else
printer.Print("float(\"-inf\")")
| Constant(value = FloatLiteral value) ->
let value = string<float> value
printer.Print(value)
Expand Down
8 changes: 4 additions & 4 deletions src/fable-library-py/fable_library/core/floats.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ def log10(x: float) -> float: ...
def log2(x: float) -> float: ...
def degrees(x: float) -> float: ...
def radians(x: float) -> float: ...
def is_nan(x: float) -> bool: ...
def is_infinity(x: float) -> bool: ...
def is_positive_infinity(x: float) -> bool: ...
def is_negative_infinity(x: float) -> bool: ...
def is_nan(x: SupportsFloat) -> bool: ...
def is_infinity(x: SupportsFloat) -> bool: ...
def is_positive_infinity(x: SupportsFloat) -> bool: ...
def is_negative_infinity(x: SupportsFloat) -> bool: ...
def floor(x: float) -> float: ...
def ceil(x: float) -> float: ...
def pow(x: float, y: float | FloatNumeric) -> float: ...
Expand Down
31 changes: 31 additions & 0 deletions tests/Python/TestArithmetic.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ let notALiteral = 5
let checkTo3dp (expected: float) actual =
floor (actual * 1000.) |> equal expected

let moduleSinglePositiveInfinity = Single.PositiveInfinity
let moduleSingleNegativeInfinity = Single.NegativeInfinity
let moduleSingleNaN = Single.NaN

[<Fact>]
let ``test Adding floats works`` () =
3.141 + 2.85 |> equal 5.991
Expand Down Expand Up @@ -750,6 +754,33 @@ let ``test sqrt matches .NET core implementation`` () =
// let result = Single.Parse("10.5", culture)
// float result |> equal 10.5

[<Fact>]
let ``test Single special values work`` () =
Single.IsPositiveInfinity Single.PositiveInfinity |> equal true
Single.IsNegativeInfinity Single.NegativeInfinity |> equal true
Single.IsInfinity Single.PositiveInfinity |> equal true
Single.IsInfinity Single.NegativeInfinity |> equal true
Single.IsNaN Single.NaN |> equal true
Single.PositiveInfinity > 0.0f |> equal true
Single.NegativeInfinity < 0.0f |> equal true
Single.PositiveInfinity |> equal infinityf
Single.NegativeInfinity |> equal -infinityf
Single.IsNaN nanf |> equal true

[<Fact>]
let ``test Single special values work as module bindings`` () =
Single.IsPositiveInfinity moduleSinglePositiveInfinity |> equal true
Single.IsNegativeInfinity moduleSingleNegativeInfinity |> equal true
Single.IsNaN moduleSingleNaN |> equal true

[<Fact>]
let ``test Double special values work`` () =
Double.IsPositiveInfinity Double.PositiveInfinity |> equal true
Double.IsNegativeInfinity Double.NegativeInfinity |> equal true
Double.IsNaN Double.NaN |> equal true
Double.PositiveInfinity |> equal infinity
Double.NegativeInfinity |> equal -infinity

[<Fact>]
let ``test acos works`` () =
acos 0.25 |> checkTo3dp 1318.
Expand Down
Loading