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

SQRT of negative numbers #2163

Closed
zx70 opened this issue Jan 20, 2023 · 4 comments
Closed

SQRT of negative numbers #2163

zx70 opened this issue Jan 20, 2023 · 4 comments

Comments

@zx70
Copy link
Member

zx70 commented Jan 20, 2023

In C the result of SQRT of negative numbers is often forced to zero.
It appears to be the preferred behavior, e.g. it fixes this: https://github.com/z88dk/z88dk-ext/blob/master/graphics/hat.c

The default high precision math library works fine, the target specific ones and the alternative ones might require a calibration.

zx70 added a commit that referenced this issue Jan 20, 2023
@feilipu
Copy link
Collaborator

feilipu commented Jan 20, 2023

For math32 the sqrt() of negative numbers is a NaN, which is as designed.

But I'm not sure NaNs are handled consistently by libraries and compilers if provided as input to a function chain. I'll need to think about that one.

zx70 added a commit that referenced this issue Jan 20, 2023
@zx70
Copy link
Member Author

zx70 commented Jan 20, 2023

I had to do something for the Sinclair ROMs, it simply interrupted the runtime by jumping to BASIC and giving an "Invalid argument" error.
The example program was originally for the MS BASIC which IMO put zero as well.
I took the opportunity to correct a bug in the "TINY" version of the ZX FP libraries.

zx70 added a commit that referenced this issue Jan 20, 2023
@feilipu
Copy link
Collaborator

feilipu commented Jan 21, 2023

Well a bit of a rough check through shows that NaN values are not generated, except for the sqrt(-n) already discussed.

The log(0) or log(-n) should return a NaN, but is currently returning negative infinity. Perhaps that could be fixed to return a NaN?

There is no NaN included in the C header files to return. Perhaps that should be added (below for IEEE754), and then returned as needed?

#define NAN_POS_F32    ((unsigned long)0x7FFFFFFF)
#define NAN_NEG_F32    ((unsigned long)0xFFFFFFFF)

The acos() and asin() functions should return a NaN when the operator is greater than 1. This could be tested and added too.

But otherwise the fsclassify() function is properly implemented, so if that is used from C then things can work as intended.

@feilipu
Copy link
Collaborator

feilipu commented Jan 21, 2023

Resolved some weaknesses with #2166.

feilipu added a commit that referenced this issue Jan 22, 2023
math32 - produce NaNs where needed - #2163
@zx70 zx70 closed this as completed Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants