mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 15:48:51 +00:00
Update fixed.odin
This commit is contained in:
@@ -29,13 +29,13 @@ Fixed52_12 :: distinct Fixed(i64, 12)
|
|||||||
|
|
||||||
|
|
||||||
init_from_f64 :: proc(x: ^$T/Fixed($Backing, $Fraction_Width), val: f64) {
|
init_from_f64 :: proc(x: ^$T/Fixed($Backing, $Fraction_Width), val: f64) {
|
||||||
i, f := math.modf(val)
|
i, f := math.modf(math.abs(val))
|
||||||
x.i = Backing(f * (1<<Fraction_Width))
|
x.i = Backing(f * (1<<Fraction_Width))
|
||||||
x.i &= 1<<Fraction_Width - 1
|
x.i &= 1<<Fraction_Width - 1
|
||||||
x.i |= Backing(i) << Fraction_Width
|
x.i |= Backing(i) << Fraction_Width
|
||||||
|
if val < 0 do x.i *= -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
init_from_parts :: proc(x: ^$T/Fixed($Backing, $Fraction_Width), integer, fraction: Backing) {
|
init_from_parts :: proc(x: ^$T/Fixed($Backing, $Fraction_Width), integer, fraction: Backing) {
|
||||||
i, f := math.modf(val)
|
i, f := math.modf(val)
|
||||||
x.i = fraction
|
x.i = fraction
|
||||||
@@ -44,9 +44,11 @@ init_from_parts :: proc(x: ^$T/Fixed($Backing, $Fraction_Width), integer, fracti
|
|||||||
}
|
}
|
||||||
|
|
||||||
to_f64 :: proc(x: $T/Fixed($Backing, $Fraction_Width)) -> f64 {
|
to_f64 :: proc(x: $T/Fixed($Backing, $Fraction_Width)) -> f64 {
|
||||||
res := f64(x.i >> Fraction_Width)
|
sign := -1.0 if x.i < 0 else 1.0
|
||||||
res += f64(x.i & (1<<Fraction_Width-1)) / f64(1<<Fraction_Width)
|
num := math.abs(x.i)
|
||||||
return res
|
res := f64(num >> Fraction_Width)
|
||||||
|
res += f64(num & (1<<Fraction_Width-1)) / f64(1<<Fraction_Width)
|
||||||
|
return res * sign
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user