Browse Source

Update fixed.odin

Justin Snyder 2 years ago
parent
commit
d4194962b0
1 changed files with 7 additions and 5 deletions
  1. 7 5
      core/math/fixed/fixed.odin

+ 7 - 5
core/math/fixed/fixed.odin

@@ -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)
-	res += f64(x.i & (1<<Fraction_Width-1)) / f64(1<<Fraction_Width)
-	return res
+	sign := -1.0 if x.i < 0 else 1.0
+	num := math.abs(x.i)
+	res := f64(num >> Fraction_Width)
+	res += f64(num & (1<<Fraction_Width-1)) / f64(1<<Fraction_Width)
+	return res * sign
 }
 }