1 people like it.

Convert integers to positive values

Convert negative and positive integers to a positive value by using an overlap and interleave scheme (more info: http://en.wikipedia.org/wiki/Golomb_coding).

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
    let toUnsigned x = 
        if x >= 0 then 
            2*x
        else
            (-2*x) - 1
    let toSigned x =
        if x % 2 = 0 then
            x/2
        else
            (x+1)/(-2)
val toUnsigned : x:int -> int

Full name: Script.toUnsigned
val x : int
val toSigned : x:int -> int

Full name: Script.toSigned
Raw view Test code New version

More information

Link:http://fssnip.net/jK
Posted:11 years ago
Author:Samuel Bosch
Tags: math