Skip to content

Casting to float32 on NimVM is broken #13479

@johnnovak

Description

@johnnovak

Example

import strutils

let u64 = 0xdeadbeefcafebabe'u64
let f64 = cast[float64](u64)
echo "*** runtime"
echo f64
echo cast[uint64](f64).toHex

let u32 = 0xdeadbeef'u32
let f32 = cast[float32](u32)
echo "*** runtime"
echo f32
echo cast[uint32](f32).toHex

static:
  let u64 = 0xdeadbeefcafebabe'u64
  let f64 = cast[float64](u64)
  echo "*** compile-time"
  echo f64
  echo cast[uint64](f64).toHex

#[
static:
  let u32 = 0xdeadbeef'u32
  let f32 = cast[float32](u32)
  echo "*** compile-time"
  echo f32
  echo cast[uint32](f32).toHex
]#

Current Output

With the last static block commented out as shown above:

*** compile-time
-1.188595878826402e+148
DEADBEEFCAFEBABE

. . .

*** runtime
-1.188595878826402e+148
DEADBEEFCAFEBABE
*** runtime
-6.259853398707798e+018
DEADBEEF

With the last static block uncommented the cast[float32](u32) statement fails. Note this only happens on NimVM and only with float32. All integer types and float64 are fine.

*** compile-time
-1.188595878826402e+148
DEADBEEFCAFEBABE
fatal.nim(64)            sysFatal
Error: unhandled exception: value out of range: 3735928559 [RangeError]

Expected Output

As explained above, casting to float32 on NimVM should work just like all the other numeric types.

Additional Information

Broken on v1.0.6 and on the latest dev build. Same results on Windows and OS X.

Nim Compiler Version 1.1.1 [Windows: amd64]
Compiled at 2020-02-23
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: fdf5480afc7fc0952332b3d5bde9c6885100da3e
active boot switches: -d:release

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions