afaik atleast in C there's no "safe" way to do it. The method presented by @e 
works in most situations, but breaks strict aliasing (a poiner of one type may 
not be casted to a pointer of an incompatible type).

The usual safer way, in C is by using a union: 
    
    
    type
        Uint8ToFloat64 {.union.} = object
            f: float64
            i: array[8, uint8]
    
    proc floatToInts(f: float64): array[8, uint8] =
        var convert: Uint8ToFloat64
        convert.f = f
        convert.i
    proc intsToFloat(i: array[8, uint8]): float64 =
        var convert: Uint8ToFloat64
        convert.i = i
        convert.f
    
    doAssert intsToFloat(floatToInts(-42.0)) == -42.0
    
    
    Run

Reply via email to