Skip to content

Commit

Permalink
Rewrite the full pi constant as acos(-1.) (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
eldritchconundrum authored May 11, 2021
1 parent 0148916 commit b8fb1e1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/parse.fs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module private ParseImpl =

let anyNumber =
let n = (hexa <|> octal <|> number) <?> "number"
// number suffixes: float, long float, unsigned, long, half. FIXME: test the last two
let suffix = ["f"; "F"; "LF"; "lf"; "u"; "U"; "l"; "L"; "h"; "H"]
|> List.map str |> choice
let suffix = suffix <?> "suffix"
Expand Down
5 changes: 5 additions & 0 deletions src/rewriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ let rec private simplifyExpr env = function
| Some (_, _, Some init) -> init
| _ -> e

// pi is acos(-1), pi/2 is acos(0)
| Float(f, _) when f = 3.141592653589793 -> FunCall(Var "acos", [Float (-1., "")])
| Float(f, _) when f = 6.283185307179586 -> FunCall(Op "*", [Float (2., ""); FunCall(Var "acos", [Float (-1., "")])])
| Float(f, _) when f = 1.5707963267948966 -> FunCall(Var "acos", [Float (0., "")])

| e -> e

// Squeeze declarations: "float a=2.; float b;" => "float a=2.,b;"
Expand Down
4 changes: 4 additions & 0 deletions tests/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@
-o tests/real/yx_long_way_from_home.frag.expected tests/real/yx_long_way_from_home.frag
-o tests/real/oscars_chair.frag.expected tests/real/oscars_chair.frag
-o tests/real/the_real_party_is_in_your_pocket.frag.expected tests/real/the_real_party_is_in_your_pocket.frag

# Optimization tests

--no-renaming --format text -o tests/unit/pi.frag.expected tests/unit/pi.frag
10 changes: 10 additions & 0 deletions tests/unit/pi.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

void main() {
double pi = 3.141592653589793;
double minus_pi = -3.141592653589793;
double tau = 6.283185307179586;
double minus_tau = -6.283185307179586;
double half_pi = 1.5707963267948966;
double minus_half_pi = -1.5707963267948966;
double precise_pi = 3.14159265358979323846264338327950288419716939937510;
}
1 change: 1 addition & 0 deletions tests/unit/pi.frag.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void main(){double pi=acos(-1.),minus_pi=-acos(-1.),tau=2.*acos(-1.),minus_tau=-(2.*acos(-1.)),half_pi=acos(0.),minus_half_pi=-acos(0.),precise_pi=acos(-1.);}

0 comments on commit b8fb1e1

Please sign in to comment.