1. Converting a (simple) program from Python to Euphoria
- Posted by ed_davis Dec 04, 2013
- 1499 views
I have a simple C program that I use for integer bench-marks, that I'd like to convert to Euphoria. Here is the C version.
#include <stdio.h> int main() { int left_edge, right_edge, top_edge, bottom_edge, max_iter, x_step, y_step, y0, x0, x, y, i, x_x, y_y, temp, the_char, accum, count; accum = 0; count = 0; while (count < 1065) { left_edge = -21000; // left edge = -2.1 right_edge = 15000; // right edge = 1.5 top_edge = 15000; // top edge = 1.5 bottom_edge = -15000; // bottom edge = -1.5 max_iter = 200; // max iteration depth x_step = 350; // x step size y_step = 750; // y step size y0 = top_edge; while (y0 > bottom_edge) { // y0 x0 = left_edge; while (x0 < right_edge) { // x0 y = 0; // y x = 0; // x the_char = ' '; // char to be displayed x_x = 0; y_y = 0; i = 0; while (i < max_iter && x_x + y_y <= 40000) { // iteration x_x = (x/10 * x) / 1000; // x*x y_y = (y/10 * y) / 1000; // y*y if (x_x + y_y > 40000) { the_char = '0' + i; // print digit 0...9 if (i > 9) { // if iteration count > 9, the_char = '@'; // print '@' } } else { temp = x_x - y_y + x0; // temp = x*x - y*y + x0 y = (x/10 * y) / 500 + y0; // y = 2*x*y + y0 x = temp; // x = temp } i = i + 1; } accum = accum + the_char; x0 = x0 + x_step; } y0 = y0 - y_step; } if (count % 100 == 0) printf("%d\n", accum); count = count + 1; } printf("%d\n", accum); return 0; }
It generates this output:
200976 20298576 40396176 60493776 80591376 100688976 120786576 140884176 160981776 181079376 201176976 214039440
I converted it to Python, and it generates the same list of numbers as above:
accum = 0 count = 0 while count < 1065: # 1065 left_edge = -21000 # left edge = -2.1 right_edge = 15000 # right edge = 1.5 top_edge = 15000 # top edge = 1.5 bottom_edge = -15000 # bottom edge = -1.5 max_iter = 200 # max iteration depth x_step = 350 # x step size y_step = 750 # y step size y0 = top_edge while y0 > bottom_edge: # y0 x0 = left_edge while x0 < right_edge: # x0 y = 0 # y x = 0 # x the_char = 32 # char to be displayed x_x = 0 y_y = 0 i = 0 while i < max_iter and x_x + y_y <= 40000: # iteration x_x = int((int(x/10)* x) / 1000) # x*x y_y = int((int(y/10)* y) / 1000) # y*y if x_x + y_y > 40000: the_char = 48 + i # print digit 0...9 if i > 9: # if iteration count > 9, the_char = 64 # print '@' else: temp = x_x - y_y + x0 # temp = x*x - y*y + x0 y = int((int(x/10) * y) / 500) + y0 # y = 2*x*y + y0 x = temp # x = temp i = i + 1 accum = accum + the_char x0 = x0 + x_step y0 = y0 - y_step if count % 100 == 0: print(accum) count = count + 1 print(accum)
Finally, I converted it to Euphoria:
integer left_edge, right_edge, top_edge, bottom_edge, max_iter, x_step, y_step, y0, x0, x, y, i, x_x, y_y, temp, the_char, accum, count accum = 0 count = 0 while count < 1065 do -- 1065 left_edge = -21000 -- left edge = -2.1 right_edge = 15000 -- right edge = 1.5 top_edge = 15000 -- top edge = 1.5 bottom_edge = -15000 -- bottom edge = -1.5 max_iter = 200 -- max iteration depth x_step = 350 -- x step size y_step = 750 -- y step size y0 = top_edge while y0 > bottom_edge do -- y0 x0 = left_edge while x0 < right_edge do -- x0 y = 0 -- y x = 0 -- x the_char = 32 -- char to be displayed x_x = 0 y_y = 0 i = 0 while i < max_iter and x_x + y_y <= 40000 do -- iteration x_x = floor((floor(x/10) * x) / 1000) -- x*x y_y = floor((floor(y/10) * y) / 1000) -- y*y if x_x + y_y > 40000 then the_char = 48 + i -- print digit 0...9 if i > 9 then -- if iteration count > 9, the_char = 64 -- print '@' end if else temp = x_x - y_y + x0 -- temp = x*x - y*y + x0 y = floor((floor(x/10) * y) / 500) + y0 -- y = 2*x*y + y0 x = temp -- x = temp end if i = i + 1 end while accum = accum + the_char x0 = x0 + x_step end while y0 = y0 - y_step end while if remainder(count, 100) = 0 then ? accum end if count = count + 1 end while ? accum
But it generates a different list of numbers:
201054 20306454 40411854 60517254 80622654 100728054 120833454 140938854 161044254 181149654 201255054 214122510
Can anyone spot where I went wrong? All 3 versions appear pretty much the same, especially the Python and Euphoria one.
Any help is appreciated!
2. Re: Converting a (simple) program from Python to Euphoria
- Posted by andi49 Dec 04, 2013
- 1439 views
Hallo
you may try:
include std/math.e
and use trunc() instead of floor()
Don't know if this is the fastest way
Andreas
3. Re: Converting a (simple) program from Python to Euphoria
- Posted by ed_davis Dec 04, 2013
- 1431 views
you may try:
include std/math.e
and use trunc() instead of floor()
Don't know if this is the fastest way
Andreas
That solved it! Thanks so much! And thanks for the very quick response!