Post Reply 
hpgcc2 loop problem
03-13-2021, 06:41 AM (This post was last modified: 03-13-2021 06:42 AM by BINUBALL.)
Post: #1
hpgcc2 loop problem
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
Code:
<< -> n
<< 0. 1. n FOR X X + NEXT
>>
>>
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.
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);
}
and based on top tutorial, I made UserRPL wrapper.
Code:
<< "Input 1 real" -> n
<< DEPTH IF 0 == THEN u DOERR END 3: "EXTEND/SM" EVAL
>>
>>
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?

S.Korean / HP-50G | fx-570EX | fx-570CW | HP-200LX
Visit this user's website Find all posts by this user
Quote this message in a reply
03-15-2021, 10:14 AM (This post was last modified: 03-15-2021 10:04 PM by brickviking.)
Post: #2
RE: hpgcc2 loop problem
From quick testing here, your C code appears to be mostly correct. As you suspected, you're seeing overflows in your output for certain large inputs.
I can't tell what your HP code is doing though, as I've never tried compiling stuff under hpgcc and executing it from the calculator.

This is the C code I used to test that things work, at least on a Linux machine (and probably a Microsoft setup too):

Code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
  int n, i;
  /* Need a bigger value for results above 2e9. May not work for hpgcc2, in this case remove unsigned keyword */
  unsigned long long sum=0;
  /* Some arg checking first */
  if (argc < 2) {
    printf("Cannot run with nothing\n");
    exit(1);
  }
  if (argc > 2) {
    printf("Too many args; I only need one number.\n");
    exit(1);
  }
  /* Absolutely no checking that argv[1] is an int */
  n = atoi(argv[1]);
  for (i=1; i<=n; i++) {
    sum += i;
  }
  /* printf need %lld because of the unsigned long long value we're printing
      You're putting value back on the stack, so calculator will deal with printing instead */
  printf("%lld\n", sum);
  exit(0);
}


(Post 336)

Regards, BrickViking
HP-50g |Casio fx-9750G+ |Casio fx-9750GII (SH4a)
Visit this user's website Find all posts by this user
Quote this message in a reply
03-15-2021, 10:22 AM
Post: #3
RE: hpgcc2 loop problem
(03-13-2021 06:41 AM)BINUBALL Wrote:  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
Code:
<< -> n
<< 0. 1. n FOR X X + NEXT
>>
>>
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.
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);
}
and based on top tutorial, I made UserRPL wrapper.
Code:
<< "Input 1 real" -> n
<< DEPTH IF 0 == THEN u DOERR END 3: "EXTEND/SM" EVAL
>>
>>
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?

Just a WAG (wild-ass guess) but could it be that you're putting a real on the stack while your routine expects an integer?

Tom L
Cui bono?
Find all posts by this user
Quote this message in a reply
03-15-2021, 11:18 AM
Post: #4
RE: hpgcc2 loop problem
(03-15-2021 10:14 AM)brickviking Wrote:  From quick testing here, your C code appears to be correct. I can't tell what your HP code is doing though, as I've never tried compiling stuff under hpgcc and executing it from the calculator.

This is the C code I used to test that things worked:


Code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
  int n, i, sum=0;
  /* Some arg checking first */
  if (argc < 2) {
    printf("Cannot run with nothing\n");
    exit(1);
  }
  if (argc > 2) {
    printf("Too many args; I only need one number.\n");
    exit(1);
  }
  /* Absolutely no checking that argv[1] is an int */
  n = atoi(argv[1]);
  for (i=1; i<=n; i++) {
    sum += i;
  }
  printf("%d\n", sum);
  exit(0);
}


(Post 336)

I've tried with online C editor too and I confirmed there is no error. Program runs well.

(03-15-2021 10:22 AM)toml_12953 Wrote:  
(03-13-2021 06:41 AM)BINUBALL Wrote:  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
Code:
<< -> n
<< 0. 1. n FOR X X + NEXT
>>
>>
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.
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);
}
and based on top tutorial, I made UserRPL wrapper.
Code:
<< "Input 1 real" -> n
<< DEPTH IF 0 == THEN u DOERR END 3: "EXTEND/SM" EVAL
>>
>>
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?

Just a WAG (wild-ass guess) but could it be that you're putting a real on the stack while your routine expects an integer?

That's a good guess. But I tried with integer 100, it gives 100 0. , therefore it doesn't compute sum.

S.Korean / HP-50G | fx-570EX | fx-570CW | HP-200LX
Visit this user's website Find all posts by this user
Quote this message in a reply
03-15-2021, 05:55 PM (This post was last modified: 03-15-2021 06:03 PM by Egan Ford.)
Post: #5
RE: hpgcc2 loop problem
Can you try to just pop/push and see if the input and output are the same? I agree it may be a float/int thing.

Also for int use n = sat_pop_zint_llong();

Try that.
Find all posts by this user
Quote this message in a reply
03-16-2021, 12:42 AM (This post was last modified: 03-16-2021 12:47 AM by BINUBALL.)
Post: #6
RE: hpgcc2 loop problem
(03-15-2021 05:55 PM)Egan Ford Wrote:  Can you try to just pop/push and see if the input and output are the same? I agree it may be a float/int thing.

Also for int use n = sat_pop_zint_llong();

Try that.

Code:
#include <hpgcc49.h>

int main(void)
{
    int n;
    // get stack 1 real number
    n = sat_pop_real();
    // output
    sat_push_real(n);
    return(0);
}

I wrote that code and bug happens too. 1000 -> 999, 1001 -> 1000..

Then what I have to use instead of sat_push_real()?
sat_push_zint()?
sat_push_zint_llong()?

S.Korean / HP-50G | fx-570EX | fx-570CW | HP-200LX
Visit this user's website Find all posts by this user
Quote this message in a reply
03-16-2021, 03:34 AM
Post: #7
RE: hpgcc2 loop problem
And, where can I find hpgcc2 functions?

S.Korean / HP-50G | fx-570EX | fx-570CW | HP-200LX
Visit this user's website Find all posts by this user
Quote this message in a reply
03-16-2021, 02:08 PM
Post: #8
RE: hpgcc2 loop problem
Did you try to format sum and n as a long integer?
Find all posts by this user
Quote this message in a reply
03-16-2021, 04:20 PM
Post: #9
RE: hpgcc2 loop problem
First thing you should try is to use hpgcc3 instead of hpgcc 2.0. It has several additional years of bug-fixing and a much more polished API to create/pop/push objects to the stack.
Find all posts by this user
Quote this message in a reply
03-17-2021, 01:19 AM
Post: #10
RE: hpgcc2 loop problem
It works properly with zint, thank you for everyone.
And, here is my last source code.
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 zint
    n = sat_pop_zint_llong();
    // calculate sum
    for (i = 1; i <= n; i++) {
        sum += i;
    }
    // output
    sat_push_zint_llong(sum);
    sys_slowOn();
    return(0);
}

S.Korean / HP-50G | fx-570EX | fx-570CW | HP-200LX
Visit this user's website Find all posts by this user
Quote this message in a reply
03-18-2021, 05:01 PM
Post: #11
RE: hpgcc2 loop problem
(03-16-2021 03:34 AM)BINUBALL Wrote:  And, where can I find hpgcc2 functions?

Try here:

https://web.archive.org/web/200904100244...hpgcc.org/
Find all posts by this user
Quote this message in a reply
03-18-2021, 05:02 PM
Post: #12
RE: hpgcc2 loop problem
(03-16-2021 04:20 PM)Claudio L. Wrote:  First thing you should try is to use hpgcc3 instead of hpgcc 2.0. It has several additional years of bug-fixing and a much more polished API to create/pop/push objects to the stack.

Does hpgcc3 still require a custom ROM?
Find all posts by this user
Quote this message in a reply
03-19-2021, 02:51 PM
Post: #13
RE: hpgcc2 loop problem
(03-18-2021 05:02 PM)Egan Ford Wrote:  Does hpgcc3 still require a custom ROM?

Yes, which is no big deal at all (never was).
The OP posted a few things about newRPL, so I don't think he's nervous about running custom ROMS (if he trusted me to completely replace his firmware, what could go wrong with a couple tiny hacks...).
Find all posts by this user
Quote this message in a reply
03-20-2021, 07:38 AM
Post: #14
RE: hpgcc2 loop problem
(03-19-2021 02:51 PM)Claudio L. Wrote:  
(03-18-2021 05:02 PM)Egan Ford Wrote:  Does hpgcc3 still require a custom ROM?

Yes, which is no big deal at all (never was).
The OP posted a few things about newRPL, so I don't think he's nervous about running custom ROMS (if he trusted me to completely replace his firmware, what could go wrong with a couple tiny hacks...).
Does HPGCC3 completely replace stock rom like newRPL? And can I go back stock rom whenever I want?
Trying HPGCC3 may not be bad for me.

S.Korean / HP-50G | fx-570EX | fx-570CW | HP-200LX
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)