Author Topic: Is this a bug?  (Read 4839 times)

0 Members and 1 Guest are viewing this topic.

Nicola

  • Guest
Is this a bug?
« on: September 02, 2020, 05:48:45 AM »
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
  1. include "$/inc/console.inc"
  2. dim b as double
  3.  
  4. while 2
  5. printl "input a number: "
  6. int a=input()
  7. b = sqrt(a)
  8. printl "a           = " a
  9. printl "b = SQRT(a) = " b
  10. printl "INT(b)      = " int(b)
  11. if b=int(b) then
  12.         printl "1) b  = int(b)"
  13. else
  14.         printl "1) b <> int(b)"
  15. end if
  16.  
  17. printl
  18.  
  19. if b - int(b) = 0 then
  20.         printl "2) b  = int(b)"
  21. else
  22.         printl "2) b <> int(b)"
  23. end if
  24.  
  25. waitkey
  26. loop
  27.  

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)


« Last Edit: September 02, 2020, 10:32:08 AM by Nicola »

Charles Pegge

  • Guest
Re: Is this a bug?
« Reply #1 on: September 02, 2020, 07:06:18 AM »
Hi Nicola,

int(n) is an uncaught error. You can have trunc() round() floor() ceil()

JRS

  • Guest
Re: Is this a bug?
« Reply #2 on: September 02, 2020, 09:01:35 AM »
FYI:

This forum supports code syntax highlighting.

Use code=o2 for OxygenBasic.

Nicola

  • Guest
Re: Is this a bug?
« Reply #3 on: September 02, 2020, 10:34:17 AM »
@Charles
Ok, but why is it okay if I do the subtraction?

Charles Pegge

  • Guest
Re: Is this a bug?
« Reply #4 on: September 02, 2020, 03:36:20 PM »
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: [Select]
b=sqrt(a0
if frac(b)
  ...
else 'no fraction
 ...
endif

Also ear in mind that square roots could be negative as well as positive.

JRS

  • Guest
Re: Is this a bug?
« Reply #5 on: September 03, 2020, 09:14:49 AM »
Hi Charles,

Does O2 have a DO keyword? I was looking for a WEND with that WHILE  and then noticed the LOOP.

Charles Pegge

  • Guest
Re: Is this a bug?
« Reply #6 on: September 03, 2020, 10:15:11 AM »
Yes John, we have do ..exit do..continue do.. end do / loop for unconditional loops

JRS

  • Guest
Re: Is this a bug?
« Reply #7 on: September 03, 2020, 04:03:14 PM »
So it's okay to use a DO keyword (LOOP) with a WHILE?

Charles Pegge

  • Guest
Re: Is this a bug?
« Reply #8 on: September 03, 2020, 05:06:22 PM »
Yes, you can use  loop to close a while block instead of wend, but I would not recommend it.

Nicola

  • Guest
Re: Is this a bug?
« Reply #9 on: September 07, 2020, 06:43:22 AM »
@Carles
Quote
Also ear in mind that square roots could be negative as well as positive.

Hi.
Yes, in the context of R, there is its result and its negative opposite whose square is the radicand. Or, the square root of a negative number has its result in scope C (complex numbers, consisting of a real part and an imaginary part) (for further information, https://en.wikipedia.org/wiki/Square_rootA may be useful )

Charles Pegge

  • Guest
Re: Is this a bug?
« Reply #10 on: September 07, 2020, 05:51:59 PM »
Speaking of complex squares, we have a number of Julia and Mandelbrot fractal examples scattered around.

Here are some fractal snaps based on various coloration settings:

https://get.google.com/albumarchive/100574392421270527709/album/AF1QipM9V1gc443sziQHkajKNSYjQ8P7nisjz__bku09

https://get.google.com/albumarchive/100574392421270527709/album/AF1QipPp5wgIYv-dkA-TD-3tGuUfLcQRFDScoE5hmtNt



JRS

  • Guest
Re: Is this a bug?
« Reply #11 on: September 07, 2020, 07:31:01 PM »
Nice fractals!

My Favorite

Nicola

  • Guest
Re: Is this a bug?
« Reply #12 on: September 08, 2020, 04:21:41 AM »
Really beautiful these fractals.  :D

... Speaking of strange things ... I was trying various systems to see how to evaluate if the result of the square root of a number n proves that it is a perfect root. Well, after some tests, when it prints the input number on the screen (printl a) some strange characters (-???<) come out ... Why?

Code: Text
  1. 'is this a bug?
  2.  
  3. include "$/inc/console.inc"
  4.  
  5. dim a,b as double
  6. global r as int
  7.  
  8. sub _print()
  9. ====================================
  10.         if r=1 then
  11.                 printl "YES is an exact root"
  12.         else
  13.                 printl "NO it is not an exact root"
  14.         end if
  15.         printl
  16.         r=0
  17. end sub
  18.  
  19.  
  20. '====== MAIN ===============
  21. do
  22.  
  23. r=0
  24. printl "input a number: "
  25. 'a=input()
  26. a = ltrim rtrim Input ' per togliere caratteri da input tipo CR
  27. if a=0 then exit do
  28. b = sqrt(a)
  29. printl "a       = " a
  30. printl "SQRT(a) = " b
  31.  
  32. printl "Text if the result = INT(result)"
  33. if b=int(b) then
  34.          r=1
  35. end if
  36. _print
  37.  
  38. printl "Text if the FRAC(result) has a fractional component"
  39. if frac(b)=0 then
  40.         r=1
  41. end if
  42. _print
  43.  
  44. printl "Text if result = TRUNC(result)"
  45. if b=trunc(b) then
  46.         r=1
  47. end if
  48. _print
  49.  
  50. printl "Test if Result - INT(result) = 0"
  51. if b - int(b) = 0 then
  52.         r=1
  53. end if
  54. _print
  55.  
  56. printl "Test if INT(Result)^2 = number in input"
  57. if int(b)*int(b) = a then
  58.         r=1
  59. end if
  60. _print
  61.  
  62. loop
  63.  
  64.  



note: using FRAC (), TRUNC () or B-int (B) the result is ok.
« Last Edit: September 08, 2020, 04:29:44 AM by Nicola »

Charles Pegge

  • Guest
Re: Is this a bug?
« Reply #13 on: September 08, 2020, 02:25:26 PM »
Hi Nicola, It works as expected. I don't see any question marks on my system. (using o2 version 0.2.9).

Nicola

  • Guest
Re: Is this a bug?
« Reply #14 on: September 09, 2020, 12:26:10 AM »
Hi Charles.
This is the version I use (see attached image).
However you have to take the test several times. The problem arises from the sixth input.