From 79da077f1a2aa6eee341be6f5dd667b0b401ab32 Mon Sep 17 00:00:00 2001 From: kolaente <k@knt.li> Date: Mon, 25 May 2020 17:54:50 +0200 Subject: [PATCH 1/5] Fix out --- newton2.hal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/newton2.hal b/newton2.hal index 72cf628..9c0a099 100644 --- a/newton2.hal +++ b/newton2.hal @@ -35,5 +35,5 @@ 42 MULNUM -1 43 ADD 10 44 JUMP 03 -45 OUT 10 +45 OUT 1 46 STOP \ No newline at end of file From 8531744e84dce442dca00003385ea3ea57ee7013 Mon Sep 17 00:00:00 2001 From: kolaente <k@knt.li> Date: Mon, 25 May 2020 17:59:28 +0200 Subject: [PATCH 2/5] Add test for newton 2 --- hal_test.go | 101 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 88 insertions(+), 13 deletions(-) diff --git a/hal_test.go b/hal_test.go index efdd4cf..4bb4fb3 100644 --- a/hal_test.go +++ b/hal_test.go @@ -115,23 +115,98 @@ func TestNewton1(t *testing.T) { err = module.Run() assert.NoError(t, err) + calculateNewton := func(start float64) float64 { + f1 := func(x float64) float64 { + return math.Pow(x, 5) + 5*math.Pow(x, 3) - 5 + } + + f1derived := func(x float64) float64 { + return 5*math.Pow(x, 4) + 15*math.Pow(x, 2) + } + + var last float64 + x := start + for x != last { + last = x + x = x - f1(x)/f1derived(x) + } + return x + } + assert.Equal(t, calculateNewton(1), module.IO[1]) } -func f1(x float64) float64 { - return math.Pow(x, 5) + 5*math.Pow(x, 3) - 5 -} +func TestNewton2(t *testing.T) { + input := []string{ + "01 START", + "02 LOADNUM 1", + "03 STORE 10", -func f1derived(x float64) float64 { - return 5*math.Pow(x, 4) + 15*math.Pow(x, 2) -} + "10 MUL 10", + "11 STORE 11", + "12 MUL 10", + "13 MUL 10", + "14 MUL 10", + "15 STORE 20", + "16 MUL 10", + "17 MULNUM 8", + "18 STORE 12", + "19 LOAD 11", + "20 MULNUM 3", + "21 ADD 12", + "22 SUBNUM 3", + "23 STORE 15", + + "24 SUB 50", + "25 JUMPNULL 45", + "26 LOAD 15", + "27 STORE 50", + + "30 LOAD 20", + "31 MULNUM 48", + "32 STORE 21", + "33 LOAD 10", + "34 MULNUM 6", + "35 ADD 21", + "36 STORE 25", + + "40 LOAD 15", + "41 DIV 25", + "42 MULNUM -1", + "43 ADD 10", + "44 JUMP 03", + + "45 LOAD 10", + "46 OUT 1", + "47 STOP", + } -func calculateNewton(start float64) float64 { - var last float64 - x := start - for x != last { - last = x - x = x - f1(x)/f1derived(x) + program, err := parser.ParseProgram(input) + assert.NoError(t, err) + + module, err := hal.NewHALModule(program, 256, 2, false) + assert.NoError(t, err) + + err = module.Run() + assert.NoError(t, err) + + calculateNewton := func(start float64) float64 { + f1 := func(x float64) float64 { + return 8*math.Pow(x, 6) + 3*math.Pow(x, 2) - 3 + } + + f1derived := func(x float64) float64 { + return 48*math.Pow(x, 5) + 6*x + } + + var last float64 + x := start + for x != last { + last = x + x = x - f1(x)/f1derived(x) + } + return x } - return x + + assert.Equal(t, calculateNewton(1), module.IO[1]) } From e33a33d4a4fbfbef5bb076c4f98630ba0d55cf0f Mon Sep 17 00:00:00 2001 From: kolaente <k@knt.li> Date: Mon, 25 May 2020 18:15:15 +0200 Subject: [PATCH 3/5] Comment all the things --- hal_test.go | 77 ++++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/hal_test.go b/hal_test.go index 4bb4fb3..2cf02d5 100644 --- a/hal_test.go +++ b/hal_test.go @@ -140,44 +140,49 @@ func TestNewton2(t *testing.T) { input := []string{ "01 START", "02 LOADNUM 1", - "03 STORE 10", - - "10 MUL 10", - "11 STORE 11", - "12 MUL 10", - "13 MUL 10", - "14 MUL 10", - "15 STORE 20", - "16 MUL 10", - "17 MULNUM 8", - "18 STORE 12", - "19 LOAD 11", - "20 MULNUM 3", - "21 ADD 12", - "22 SUBNUM 3", - "23 STORE 15", - - "24 SUB 50", - "25 JUMPNULL 45", - "26 LOAD 15", + "03 STORE 10", // Every x is saved to reg 10 + + // f(x) + "10 MUL 10", // x² + "11 STORE 11", // Save x² to reg 11 for later use since we need that in the second part of the function + "12 MUL 10", // x³ + "13 MUL 10", // x⁴ + "14 MUL 10", // x⁵ + "15 STORE 20", // This is x⁵ which we save here to save a few instructions + "16 MUL 10", // x⁶ + "17 MULNUM 8", // x⁶ * 8 + "18 STORE 12", // Reg 12 now contains the result of the first part of f(x) + "19 LOAD 11", // Put x² from earlier back in the accumulator + "20 MULNUM 3", // x² * 3 + "21 ADD 12", // Add the first and second part of the function together, basically 8x⁶ + 3x² + "22 SUBNUM 3", // (8x⁶ + 3x²) - 3 + "23 STORE 15", // Result in reg 15 + + // This is our exit condition: + // We compare the current result with the last calculated result, if both are equal, we exit + "24 SUB 50", // 50 contains the last result + "25 JUMPNULL 45", // If both are equal, we exit + "26 LOAD 15", // Otherwise load reg 15 again, store it as the current last result and continue "27 STORE 50", - "30 LOAD 20", - "31 MULNUM 48", - "32 STORE 21", - "33 LOAD 10", - "34 MULNUM 6", - "35 ADD 21", - "36 STORE 25", - - "40 LOAD 15", - "41 DIV 25", - "42 MULNUM -1", - "43 ADD 10", - "44 JUMP 03", - - "45 LOAD 10", - "46 OUT 1", + // f'(x) + "30 LOAD 20", // x⁵ which we saved earlier while calculating x⁶ + "31 MULNUM 48", // x⁵ * 48 + "32 STORE 21", // Store the result in 21 + "33 LOAD 10", // Load our x + "34 MULNUM 6", // x * 6 + "35 ADD 21", // (x * 6) + 48x⁵ + "36 STORE 25", // Store the result, which is the total result of f'(x) in reg 25 + + // x - f(x) / f'(x) + "40 LOAD 15", // load f(x) + "41 DIV 25", // f(x) / f'(x) + "42 MULNUM -1", // Invert the result + "43 ADD 10", // Add x: Because we inverted the result before, we can do this instead of SUB - lets us reuse the content of the accumulator + "44 JUMP 03", // Go to the beginning + + "45 LOAD 10", // Load the result + "46 OUT 1", // Print t "47 STOP", } From 8bfcbec08aa4583bd593bde4b9916cc75e3c6297 Mon Sep 17 00:00:00 2001 From: kolaente <k@knt.li> Date: Mon, 25 May 2020 18:19:35 +0200 Subject: [PATCH 4/5] Fix comparing result --- hal_test.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/hal_test.go b/hal_test.go index 2cf02d5..09a6d56 100644 --- a/hal_test.go +++ b/hal_test.go @@ -158,13 +158,6 @@ func TestNewton2(t *testing.T) { "22 SUBNUM 3", // (8x⁶ + 3x²) - 3 "23 STORE 15", // Result in reg 15 - // This is our exit condition: - // We compare the current result with the last calculated result, if both are equal, we exit - "24 SUB 50", // 50 contains the last result - "25 JUMPNULL 45", // If both are equal, we exit - "26 LOAD 15", // Otherwise load reg 15 again, store it as the current last result and continue - "27 STORE 50", - // f'(x) "30 LOAD 20", // x⁵ which we saved earlier while calculating x⁶ "31 MULNUM 48", // x⁵ * 48 @@ -179,11 +172,22 @@ func TestNewton2(t *testing.T) { "41 DIV 25", // f(x) / f'(x) "42 MULNUM -1", // Invert the result "43 ADD 10", // Add x: Because we inverted the result before, we can do this instead of SUB - lets us reuse the content of the accumulator - "44 JUMP 03", // Go to the beginning - "45 LOAD 10", // Load the result - "46 OUT 1", // Print t - "47 STOP", + "44 STORE 35", // Reg 35 now contains the end result of x - f(x) / f'(x) + + // This is our exit condition: + // We compare the current result with the last calculated result, if both are equal, we exit + "50 SUB 50", // Reg 50 contains the last result + "51 JUMPNULL 60", // If both are equal, we exit + "52 LOAD 35", // Otherwise load reg 15 again, store it as the current last result and continue + "53 STORE 50", + + // Go to the beginning + "54 JUMP 03", + + "60 LOAD 10", // Load the result + "61 OUT 1", // Print it + "62 STOP", } program, err := parser.ParseProgram(input) From f3442e59b822140a462248d6cdd0b1a9189c0a55 Mon Sep 17 00:00:00 2001 From: kolaente <k@knt.li> Date: Mon, 25 May 2020 18:22:27 +0200 Subject: [PATCH 5/5] Fix newton2.hal --- newton2.hal | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/newton2.hal b/newton2.hal index 9c0a099..3e51a0b 100644 --- a/newton2.hal +++ b/newton2.hal @@ -17,11 +17,6 @@ 22 SUBNUM 3 23 STORE 15 -24 SUB 50 -25 JUMPNULL 45 -26 LOAD 15 -27 STORE 50 - 30 LOAD 20 31 MULNUM 48 32 STORE 21 @@ -34,6 +29,16 @@ 41 DIV 25 42 MULNUM -1 43 ADD 10 -44 JUMP 03 -45 OUT 1 -46 STOP \ No newline at end of file + +44 STORE 35 + +50 SUB 50 +51 JUMPNULL 60 +52 LOAD 35 +53 STORE 50 + +54 JUMP 03 + +60 LOAD 10 +61 OUT 1 +62 STOP