0 people like it.

Is 1.0 identity element of multiplication?

A simple script that checks whether 1.0 is really the identity element of multiplication for 32bit floating-point numbers.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
open System

// Step 100 takes about 3 sec on my machine
// so we can iterate over all flaot32 values
// in something like 5 minutes :-)
let step = 100 

// This can quite likely be optimized too :-)
for v in Int32.MinValue .. step .. Int32.MaxValue do
  let f = BitConverter.ToSingle(BitConverter.GetBytes(v), 0)
  // If they are both NaN, then that's okay, but since
  // NaN <> NaN, we need to check that case explicitly
  if Single.IsNaN(f) <> Single.IsNaN(1.0f * f) && 
     f <> 1.0f * f then 
    failwithf "Failed for: %f" f
namespace System
val step : int

Full name: Script.step
val v : int
type Int32 =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 3 overloads
    static val MaxValue : int
    static val MinValue : int
    static member Parse : s:string -> int + 3 overloads
    static member TryParse : s:string * result:int -> bool + 1 overload
  end

Full name: System.Int32
field int.MinValue = -2147483648
field int.MaxValue = 2147483647
val f : float32
type BitConverter =
  static val IsLittleEndian : bool
  static member DoubleToInt64Bits : value:float -> int64
  static member GetBytes : value:bool -> byte[] + 9 overloads
  static member Int64BitsToDouble : value:int64 -> float
  static member ToBoolean : value:byte[] * startIndex:int -> bool
  static member ToChar : value:byte[] * startIndex:int -> char
  static member ToDouble : value:byte[] * startIndex:int -> float
  static member ToInt16 : value:byte[] * startIndex:int -> int16
  static member ToInt32 : value:byte[] * startIndex:int -> int
  static member ToInt64 : value:byte[] * startIndex:int -> int64
  ...

Full name: System.BitConverter
BitConverter.ToSingle(value: byte [], startIndex: int) : float32
BitConverter.GetBytes(value: float) : byte []
BitConverter.GetBytes(value: float32) : byte []
BitConverter.GetBytes(value: uint64) : byte []
BitConverter.GetBytes(value: uint32) : byte []
BitConverter.GetBytes(value: uint16) : byte []
BitConverter.GetBytes(value: int64) : byte []
BitConverter.GetBytes(value: int) : byte []
BitConverter.GetBytes(value: int16) : byte []
BitConverter.GetBytes(value: char) : byte []
BitConverter.GetBytes(value: bool) : byte []
type Single =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 3 overloads
    static val MinValue : float32
    static val Epsilon : float32
    static val MaxValue : float32
    static val PositiveInfinity : float32
    static val NegativeInfinity : float32
    ...
  end

Full name: System.Single
Single.IsNaN(f: float32) : bool
val failwithf : format:Printf.StringFormat<'T,'Result> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.failwithf
Raw view Test code New version

More information

Link:http://fssnip.net/n3
Posted:10 years ago
Author:Tomas Petricek
Tags: floating point