Programming > Bugs & Feature Requests
Is this a bug?
Nicola:
Hi.
You've probably discussed this before, but I've happened to see a result that looks anomalous.
If I take the square root of 25, for example, the result is different from the integer (INT) of the result itself. While if I do the result of the root minus the integer of the result itself, exactly 0 comes out.
I am attaching the program with which I tested the anomaly.
Why does this happen?
--- Code: OxygenBasic ---include "$/inc/console.inc"dim b as double while 2printl "input a number: "int a=input()b = sqrt(a)printl "a = " aprintl "b = SQRT(a) = " bprintl "INT(b) = " int(b)if b=int(b) then printl "1) b = int(b)"else printl "1) b <> int(b)"end if printl if b - int(b) = 0 then printl "2) b = int(b)"else printl "2) b <> int(b)"end if waitkeyloop
This is the result of the tests:
input a number: 25
a = 25
b = SQRT(a) = 5
INT(b) = 5
1) b <> int(b)
2) b = int(b)
input a number: 63
a = 63
b = SQRT(a) = 7.9372539331937721
INT(b) = 8
1) b <> int(b)
2) b <> int(b)
(same result, ok)
Charles Pegge:
Hi Nicola,
int(n) is an uncaught error. You can have trunc() round() floor() ceil()
JRS:
FYI:
This forum supports code syntax highlighting.
Use code=o2 for OxygenBasic.
Nicola:
@Charles
Ok, but why is it okay if I do the subtraction?
Charles Pegge:
I'm not sure, but in general it is unsafe to compare integers and floats in a conditional. It is also unsafe to compare floats with floats for equality where expressions are involved.
You could use frac() to look at the fractional component of a float, as well as trunc() to get the integer component.
--- Code: ---b=sqrt(a0
if frac(b)
...
else 'no fraction
...
endif
--- End code ---
Also ear in mind that square roots could be negative as well as positive.
Navigation
[0] Message Index
[#] Next page
Go to full version