![]() |
|
Working with NAN - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Working with NAN (/showthread.php?tid=1976) Pages:
1
2
|
Working with NAN - Dimster - 09-08-2023 My memory is sharp but short. Where a same number is divided by itself the resultant is 1 ( ie 1/1 , 2/2 etc) in keeping with this I'm trying to catch a NAN result and convert it to the value of 1. So NAN doesn't seem to be a numeric nor a string. Is there a way to trap NAN and force a value of 1 ?? Code: (Select All) a = 0RE: Working with NAN - DSMan195276 - 09-08-2023 An easy way to test for NaN is to compare the result against itself - NaN is the only floating-point value which is not equal to itself. That said, arguably the better way to handle this is to check for the NaN-causing situations before doing the math. RE: Working with NAN - mnrvovrfc - 09-08-2023 Pure Basic has a NAN "function". The trick is to find an especially-encoded 32-bit or 64-bit floating-point value to represent NAN. Find two others to represent positive and negative infinity. Maybe another one to represent the square root of -1. And so on. FROM: https://en.wikipedia.org/wiki/NaN#Floating_point Quote:IEEE 754 NaNs are encoded with the exponent field filled with ones (like infinity values), and some non-zero number in the significand field (to make them distinct from infinity values); this allows the definition of multiple distinct NaN values, depending on which bits are set in the significand field, but also on the value of the leading sign bit (but applications are not required to provide distinct semantics for those distinct NaN values). RE: Working with NAN - Dimster - 09-08-2023 Would this be a success trap for NAN. I'm not sure if the IF statement is proving the NONE existence of a valid number (ie testing for NAN) or no matter what the value of a/b they will simply be forced to their new values of 1/1. I'm looking for this algorythm to only trigger if a/b = NAN. Code: (Select All) a = 0RE: Working with NAN - mnrvovrfc - 09-08-2023 Must do things this way instead as DSMan195276 said: Code: (Select All) dim as double a, bMight have to make the factor even larger to cover more decimal places for double-precision floating point. It's not as elegant as "a / b" but there is no helping it if "b" is zero or very darned close to zero. Could go further in another application: Code: (Select All) function newtan# (degrees as double)The "altsqrt()" isn't a very good example. Instead it should be able to come up with something like "2 + 3i" with "real" being 2 and "imag" being 3. RE: Working with NAN - DSMan195276 - 09-08-2023 (09-08-2023, 06:06 PM)Dimster Wrote: Would this be a success trap for NAN. ...That won't work because NaN is "falsey", in an expression like that it turns into a false value. You could use this with `Int()` to turn NaN into zero and do some comparisons, but it would get pretty messy. Not to repeat myself, but you can do it like this, it looks silly but it does work ``` a = 0 b = 0 ret = a / b If ret <> ret Then ' NaN is the only value that does not equal itself ret = 1 End If Print ret ``` RE: Working with NAN - Dimster - 09-09-2023 Thank you very much mnrvo & DS. Great ideas here to work with. It's hard to believe a math formula could actually come up with a None Number value (Not a Number). I guess it all boils down to if zero is a numeric value or a place holder for a void in the infinite number set. Logically speaking division by zero shouldn't be infinity either. If zero is a void in the infinite number set (which a NaN result implies), then when we divide 3 apples among 0 people the resultant should be 3, not infinity. I understand the history of math credits the Indians with the introduction of zero as a number (and introduced negative numbers) but before that zero was a void. So if zero is not a void and is a real number, then NaN should not exist. End of my rant on NaN. RE: Working with NAN - DSMan195276 - 09-09-2023 (09-09-2023, 01:42 PM)Dimster Wrote: Thank you very much mnrvo & DS. Great ideas here to work with. I might be misunderstanding you, but I would note that NaN is not infinity or negative infinity, those both have separate floating point values. NaN simply means that there is no answer to what you're trying to do (Ex. distribute 3 apples among 0 people). There are other ways to get NaN, such as taking the square root of a negative, they're all actions that don't result in an answer. I would also consider that if you allow division by zero to equal anything you break math Consider this:Code: (Select All) c=a/bRE: Working with NAN - Dimster - 09-09-2023 Hi DS I do follow your math and I do understand the points you are making but... in terms of an AI program, where logic is a key element, NaN screws it all up. In my apple math of 3 apples divided by zero, I didn't mean that math to equal NaN but rather equal infinity. So I do understand that if you divide the 3 apples with the numeric value of zero it goes into it an infinite number of times. I can write code which makes sure my math formulas do not divide by zero, so in effect controlling infinity. But NaN is a different story. You would think it should also be a simple matter of making sure both my numerator and denominator can not be zero at the same time but as you point out, this is not the only condition which leads to a NaN result. Thanks for the discussion, eases the frustration and gets the thinking cap back on. RE: Working with NAN - SMcNeill - 09-09-2023 Save as isnan.h: Code: (Select All) #include <stdio.h>Code: (Select All)
And now you can test for Nan, if you decide to. (There's also isinf which you'd basically implement the same way if you needed it, which tells you if a value is INF or not. If you need it as well, and can't sort it out on your own, I'll happily toss it into a simple header file for you.) |