Subroutines, arguments, and negative values

I know that, on the whole, CS is not designed to play nicely with negative numbers. I’m not asking about assigning negative values to stats. That’s not my problem.

My problem is that I have a subroutine that receives numeric values, and fairmath adds them to different stats, depending on the values of certain game flags. So far I have only tested it with addition. For subtraction, I planned to use the same subroutine, but pass along negative integer values. Basically, %- 1 should be the same as %+ (-1). I mean, arithmetically they’re equivalent, right? The end values would still conform to the standard 0-100 for CS.

That bit of code gave me some trouble late last night, and it occurred to me this morning that CS does not play nice with negative values. So I thought the smartest thing would be to ask at the source. Can CS subroutines receive negative integers as arguments? Or do I need to duplicate my subroutine and create a decrementor?

ETA: This is the text of the error:

Error: 2heir line 1473: Invalid expression at char 3, expected NUMBER, STRING, VAR or PARENTHETICAL, was: OPERATOR [-]

2 Likes

Can you give an example of part of the code that was being problematic?

1 Like

I have many sub calls that look like this:

*gosub trustme 5 1 0

They work fine and have for weeks now.

I tried adding a call that looks like this:

*gosub trustme -5 -1 0

It failed quicktest and I had to comment it out.

1 Like

Try passing the negative numbers as strings…
*gosub… “-1”

I haven’t tried it but you might find it converts.

6 Likes

My suggestion, without seeing the rest of the code, would be to do something like…

*gosub trustme (0 - 5) (0 - 1) 0

instead of

*gosub trustme -5 -1 0

I also have no idea if this will work. :grin:

I really like @CJW’s string theory…
Physics? What?

3 Likes

Okay, I’m gonna try both of those and report back in an hour.

1 Like

ChoiceScript is built on JavaScript which is weakly typed and capable of implicit conversions between types. I think in this case I am mistaken though (I just tried it in a JS console), it seems to prefer a string in this scenario. @Carlos.R’s idea could very well work though, and if it doesn’t just do:

*temp num 10
*temp neg 0-num
*gosub... neg
2 Likes

I can cheerfully report that both methods work as intended. At least with regular subtraction. I will have to run a more rigorous test on fairmath later on.

It seems the core of the problem is not to do with weak typing, but just that whatever parses CS code does not recognize the negative symbol as anything but an arithmetic operator. Judging from the text of the error message, that is.

7 Likes

facepalm never thought to cast negative numbers as a string before. Seems obvious now. I’ve been using the (0 - 1) trick myself. Its definitely the parser that causes it. Having programmed an expression parser in other languages before, I can attest to the fact that implementing the ability to use negative numbers isn’t a simple task. While it would be a nice addition to ChoiceScript, I can understand why the devs don’t want to go through the hassle of implementing it. It’s a lot of effort for a feature not a lot of people will use.

3 Likes