CPC Basic

Would you like to react to this message? Create an account in a few clicks or log in to continue.
CPC Basic

Basic cross-compiler for Amstad CPC


2 posters

    |SUB bug (I'm certain this time!)

    avatar
    poppichicken


    Posts : 16
    Join date : 2012-03-16

    |SUB bug (I'm certain this time!) Empty |SUB bug (I'm certain this time!)

    Post  poppichicken Thu Apr 26, 2012 11:21 am

    Hi there.

    I was definitely wrong about there being a bug in |MUL in my previous post, but it appears that something may be wrong with SUB.

    This program is not yet complete (it doesn't plot anything on screen yet), but it calculates points to display a 3d cone.

    Code:
    10 piDouble=0:piDouble61=1:const61=2:piDouble193=3:const193=4:piHalf=5:const2=6:piOffset=7
    20 sin1=8:const1=9:const30=10:tmpY=11:const119=12:sin2=13:tmpYb=14:const400=15
    50 |PI,piDouble:|ADD,piDouble,piDouble,piDouble
    51 |CREAL,const61,61:|DIV,piDouble61,piDouble,const61
    52 |CREAL,const193,193:|DIV,piDouble193,piDouble,const193
    53 |CREAL,const2,2:|PI,piHalf:|DIV,piHalf,piHalf,const2
    54 |SUB,piOffset,piDouble,piHalf
    55 |CREAL,const1,1
    56 |CREAL,const30,30
    57 |CREAL,const119,119
    58 |CREAL,const400,400
    100 MODE 2:PAPER 0:CLS:INK 0,0
    200 FOR a=0 TO 61 STEP 2
    210 |CREAL,sin1,a:|MUL,sin1,sin1,piDouble61:|ADD,sin1,sin1,piOffset:|SIN,sin1,sin1:|ADD,sin1,sin1,const1
    300 FOR v=0 TO 193':LOCATE 1,1:PRINT v
    310 |CREAL,sin2,v:|MUL,sin2,sin2,piDouble193:|ADD,sin2,sin2,piOffset:|SIN,sin2,sin2:|ADD,sin2,sin2,const1
    400 |MUL,sin2,sin1,sin2:|MUL,sin2,sin2,const30:|CREAL,tmpY,a:|ADD,tmpY,tmpY,const119:|SUB,tmpYb,tmpY,sin2
    401 |SUB,tmpY,tmpY,sin2
    410 '|CINT,@py,tmpYb:PRINT py;" ";
    420 |CINT,@py,tmpY:PRINT py;" ";
    500 NEXT:NEXT
    Line 400 performs several calculations, using sin2 and tmpY as storage variables.

    There is a command: |SUB,tmpYb,tmpY,sin2
    i.e. tmpYb=tmpY-sin2

    When I print tmpYb in line 410, I get 119 appearing several times, then 121, then 123 etc.
    This is correct.

    But if I use the code in line 401: |SUB,tmpY,tmpY,sin2
    i.e. tmpY=tmpY-sin2

    I only get zeroes being printed by line 420.

    So it appears that |SUB is flawed when a variable is subtracting something from itself.

    I can certainly get around this problem by using tmpYb - I just thought you'd like to know.
    I don't know if any of the other RSX commands have similar problems when a variable operates on itself.

    Using |ADD for tmpY to add something to itself seems to be OK though...
    avatar
    Dinoneno


    Posts : 10
    Join date : 2012-03-11

    |SUB bug (I'm certain this time!) Empty Re: |SUB bug (I'm certain this time!)

    Post  Dinoneno Tue May 01, 2012 7:16 am

    I think I've found the problem. Of all the firmware math, subtraction operates on values reversed, and by poor design in writing the command RSX | SUB I did not consider this problem, which occurs when the first operand and the result is the same. Can be solved by storing the result in a real element than the first operand, but I hope soon to rewrite the command to correct this problem.
    avatar
    poppichicken


    Posts : 16
    Join date : 2012-03-16

    |SUB bug (I'm certain this time!) Empty Re: |SUB bug (I'm certain this time!)

    Post  poppichicken Tue May 01, 2012 1:01 pm

    Ah, I see.
    That's ok, it's an easy problem to work around.

    Another question if I may (sorry about this!):

    I'm trying to understand the differences between |CREAL and |CVAL.

    Code:
    10 varL=0:oneThird=1:result=2
    20 |CREAL,varL,18
    30 '|CREAL,oneThird,0.333
    40 |VAL,oneThird,"0.333"
    50 |MUL,result,varL,oneThird
    60 |CINT,@tmp,result
    70 PRINT tmp
    With the code above, 6 is printed, which is correct.
    But if I remove line 40, and uncomment line 30, 0 is printed.
    Does that mean that |CREAL is ONLY for putting integer numbers into a real variable?

    Thanks.
    avatar
    Dinoneno


    Posts : 10
    Join date : 2012-03-11

    |SUB bug (I'm certain this time!) Empty Re: |SUB bug (I'm certain this time!)

    Post  Dinoneno Tue May 01, 2012 9:25 pm

    Good question Laughing

    When you use |CREAL, the numeric parameter you give is automatically converted to an integer before the command to start treating it. This makes it also Locomotive Basic, when you give a parameter non integer to CALL or a RSX command value is converted to an integer rounding, for example 0.33 -> 0, 0.75 -> 1. Also same for a parameter for a Basic command that use a integer value, for example MOVE, PLOT, ...

    If you are using |VAL what you send to the command a string, not rounded or converted to any other type. And it is the VAL command which evaluates the string as it is to obtain the corresponding real value.
    avatar
    poppichicken


    Posts : 16
    Join date : 2012-03-16

    |SUB bug (I'm certain this time!) Empty Re: |SUB bug (I'm certain this time!)

    Post  poppichicken Tue May 01, 2012 11:41 pm

    Ah, that all makes sense. Thanks.
    Does there need to be more information on the documentation page, to make things like that clearer?

    Sponsored content


    |SUB bug (I'm certain this time!) Empty Re: |SUB bug (I'm certain this time!)

    Post  Sponsored content


      Current date/time is Fri Mar 29, 2024 12:56 am