03-13-2021, 06:41 AM
I use HPGCC2 and this tutorial to make sum program for 50g.
http://sense.net/~egan/hpgcc/
example, This sum program made in UserRPL looks like this
It's simple program, which gets stack 1 number and calculate 1 to that number sum.
so, I want to C's speed. I coded this.
and based on top tutorial, I made UserRPL wrapper.
where "EXTEND/SM" is C program's location.
But I execute this program to calculate 1 to 1000 sum, It gives 499500 instead of 500500. and number goes up over 1000, It gives wrong answer.
ex)
input -> answer
999 -> 499,500 (correct)
1,000 -> 499,500 (incorrect which is 500,500)
1,001 -> 500,500 (incorrect which is 501,501)
2,000 -> 1,999,000 (incorrect which is 2,001,000)
10,000,000 -> -2,001,260,032 (I think it is overflow)
I'm a very noob for C, so my code may be wrong. What's wrong with my program?
http://sense.net/~egan/hpgcc/
example, This sum program made in UserRPL looks like this
Code:
<< -> n
<< 0. 1. n FOR X X + NEXT
>>
>>
so, I want to C's speed. I coded this.
Code:
#include <hpgcc49.h>
int main(void)
{
// sum 1 to n and counter i
int n, i;
int sum = 0;
// fast mode
sys_slowOff();
// get stack 1 real number
n = sat_pop_real();
// calculate sum
for (i = 1; i <= n; i++) {
sum += i;
}
// output
sat_push_real(sum);
sys_slowOn();
return(0);
}
Code:
<< "Input 1 real" -> n
<< DEPTH IF 0 == THEN u DOERR END 3: "EXTEND/SM" EVAL
>>
>>
But I execute this program to calculate 1 to 1000 sum, It gives 499500 instead of 500500. and number goes up over 1000, It gives wrong answer.
ex)
input -> answer
999 -> 499,500 (correct)
1,000 -> 499,500 (incorrect which is 500,500)
1,001 -> 500,500 (incorrect which is 501,501)
2,000 -> 1,999,000 (incorrect which is 2,001,000)
10,000,000 -> -2,001,260,032 (I think it is overflow)
I'm a very noob for C, so my code may be wrong. What's wrong with my program?