Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] More built-in math functions (MIN, MAX, ABS/SGN) #723

Closed
3 tasks
Rangi42 opened this issue Feb 1, 2021 · 3 comments
Closed
3 tasks

[Feature request] More built-in math functions (MIN, MAX, ABS/SGN) #723

Rangi42 opened this issue Feb 1, 2021 · 3 comments
Labels
enhancement Typically new features; lesser priority than bugs rgbasm This affects RGBASM

Comments

@Rangi42
Copy link
Contributor

Rangi42 commented Feb 1, 2021

  • MIN(args...) = the minimum one of args (must be at least one)
  • MAX(args...) = the maximum one of args (must be at least one)
  • SGN(N) = 1, 0, or -1, depending on the sign of N
  • ABS(N) = the absolute value of N (this is equivalent to N * SGN(N))

I frequently write conditionals like these:

; N = min(..., 42)
N = ...
if N > 42
N = 42
endc
; N = max(..., 0)
N = ...
if N < 0
N = 0
endc
; N = abs(...)
N = ...
if N < 0
N = -N
endc
; X = sgn(N)
X = 0
if N > 0
X = 1
elif N < 0
X = -1
endc
; now use X as a factor in further calculations

In theory, user-defined functions will eventually allow these. However, built-in functions would not have to wait for that, and would reserve an obvious name for them. Even after user-defined functions are implemented, MIN and MAX would be difficult to define like this without vararg, recursion, overloading, and ternary support.

def min(a) = a
def min(a, b) = a < b ? a : b
def min(a, b, ...) = min(min(a, b), ...)

def max(a) = a
def max(a, b) = a > b ? a : b
def max(a, b, ...) = max(max(a, b), ...)

def sgn(n) = n > 0 ? 1 : n < 0 ? -1 : 0
def abs(n) = n * sgn(n)

def abs(n) = n > 0 ? n : -n
def sgn(n) = n != 0 ? n / abs(n) : 0
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Feb 3, 2021
@ISSOtm
Copy link
Member

ISSOtm commented Feb 10, 2021

Since there is a user-defined alternative, why make them built-in? If they're found to be used really often and to be performance hits, then we'll consider making them built-in. (We should suggest in the docs to only define them if not already defined, for future-proofing)

IF !DEF(min)
def min(a) = a
; ...
ENDC

While we're at this, how should function names be standardized?

@ISSOtm ISSOtm added enhancement Typically new features; lesser priority than bugs rgbasm This affects RGBASM labels Feb 10, 2021
@Rangi42
Copy link
Contributor Author

Rangi42 commented Feb 10, 2021

There will be a user-defined alternative eventually, assuming varargs, recursion, overloading, and the ternary operator are all implemented. Until then, you still have to do if X < 42 \ X = 42 \ endc. I've seen enough such greater/less-than checks that I think MIN+MAX would be worthwhile, to write one line instead of three.

For standardized function names, so far it seems like rgbasm has gone with the typical or straightforward names for built-in functions (SIN, ATAN2, BANK, etc), with a convention of STR-prefixing all string-related functions.

Rangi42 added a commit to Rangi42/rgbds that referenced this issue Feb 10, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Feb 11, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Feb 12, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Feb 17, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Feb 17, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Feb 17, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Feb 17, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Feb 19, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Feb 21, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Feb 28, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Mar 3, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Mar 5, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Mar 5, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Mar 10, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Mar 26, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Mar 28, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Mar 28, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Mar 28, 2021
@Rangi42 Rangi42 changed the title [Feature request] MIN and MAX functions [Feature request] More built-in math functions (MIN, MAX, ABS) Mar 31, 2021
@Rangi42 Rangi42 changed the title [Feature request] More built-in math functions (MIN, MAX, ABS) [Feature request] More built-in math functions (MIN, MAX, ABS/SGN) Mar 31, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Mar 31, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Apr 7, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Apr 13, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Apr 17, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Apr 17, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Apr 19, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Apr 23, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Apr 28, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Apr 29, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue May 4, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue May 5, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue May 9, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue May 22, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Jun 24, 2021
Rangi42 added a commit to Rangi42/rgbds that referenced this issue Jun 27, 2021
@Rangi42
Copy link
Contributor Author

Rangi42 commented Sep 28, 2022

All the built-in functions so far are for operations that would be difficult or impossible to do with the available integer operators; in particular the fixed-point math and trigonometry functions. There are a lot of useful small functions on integers, and it's probably simplest to leave those for user-defined (once that's possible).

@Rangi42 Rangi42 closed this as completed Sep 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Typically new features; lesser priority than bugs rgbasm This affects RGBASM
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants