Oxygen Basic

Programming => Bugs & Feature Requests => Topic started by: Nicola on September 02, 2020, 05:48:45 AM

Title: Is this a bug?
Post by: Nicola 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)


Title: Re: Is this a bug?
Post by: Charles Pegge on September 02, 2020, 07:06:18 AM
Hi Nicola,

int(n) is an uncaught error. You can have trunc() round() floor() ceil()
Title: Re: Is this a bug?
Post by: JRS on September 02, 2020, 09:01:35 AM
FYI:

This forum supports code syntax highlighting.

Use code=o2 for OxygenBasic.
Title: Re: Is this a bug?
Post by: Nicola on September 02, 2020, 10:34:17 AM
@Charles
Ok, but why is it okay if I do the subtraction?
Title: Re: Is this a bug?
Post by: Charles Pegge 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.
Title: Re: Is this a bug?
Post by: JRS 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.
Title: Re: Is this a bug?
Post by: Charles Pegge on September 03, 2020, 10:15:11 AM
Yes John, we have do ..exit do..continue do.. end do / loop for unconditional loops
Title: Re: Is this a bug?
Post by: JRS on September 03, 2020, 04:03:14 PM
So it's okay to use a DO keyword (LOOP) with a WHILE?
Title: Re: Is this a bug?
Post by: Charles Pegge 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.
Title: Re: Is this a bug?
Post by: Nicola 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 )
Title: Re: Is this a bug?
Post by: Charles Pegge 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


Title: Re: Is this a bug?
Post by: JRS on September 07, 2020, 07:31:01 PM
Nice fractals!

My Favorite (https://get.google.com/albumarchive/100574392421270527709/album/AF1QipM9V1gc443sziQHkajKNSYjQ8P7nisjz__bku09/AF1QipMyhTm64i44vOkgMm_SA86oskg48XadXr_k2Rym)
Title: Re: Is this a bug?
Post by: Nicola 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.
Title: Re: Is this a bug?
Post by: Charles Pegge 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).
Title: Re: Is this a bug?
Post by: Nicola 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.
Title: Re: Is this a bug?
Post by: Charles Pegge on September 09, 2020, 01:19:26 AM
Even after a number of repeats, it does not happen.

Could you try inserting a cls here:
Code: [Select]
    a = ltrim rtrim Input ' per togliere caratteri da input tipo CR
    if a=0 then exit do
cls
    b = sqrt(a)
Title: Re: Is this a bug?
Post by: Nicola on September 10, 2020, 01:16:11 AM
First of all thank you for the time you are dedicating to me ...
Unfortunately it also happens to me by entering CLS. I tried on both a 64bit and a 32bit.
See the attached file. I recorded the inputs until the weird characters come out.
Title: Re: Is this a bug?
Post by: Charles Pegge on September 10, 2020, 01:51:42 AM
Hi Nicola,

Your mp4 seems to be blank.

But there is nothing obvious wrong with your program, and I don't think anyone here has encountered that kind of problem with console outputs. I can only suggest stripping down the program to find out more about what causes this bug on your system.
Title: Re: Is this a bug?
Post by: Nicola on September 10, 2020, 02:11:23 AM
the video is compressed with x264. Try using a video player that supports it. I use MPC-HC
Title: Re: Is this a bug?
Post by: Charles Pegge on September 10, 2020, 02:51:59 AM
Ok, mpc works.
Title: Re: Is this a bug?
Post by: jack on September 10, 2020, 03:06:05 AM
@Nicola
I would like to help in determining what the problem is, what are the specs of your computer and OS?
I can't reproduce the problem on my PC, could you attach the exe that produces the problem?
Title: Re: Is this a bug?
Post by: Nicola on September 10, 2020, 04:10:33 AM
@jack
see attachments.

As I wrote before, I tried both compiling for a 32 bit and for a 64 bit. In both cases there is the same drawback. The 32-bit I tried it on a 32-bit win7 operating system.
This is for 32bit
Title: Re: Is this a bug?
Post by: Charles Pegge on September 10, 2020, 06:40:10 AM
Try this prog and see if there are any problems with it:

Code: [Select]
uses console
int i
double r
print "integer square roots" cr cr
for i=1 to 1000
  r=sqr(i)
  if frac(r)=0.0
    print i tab r cr
  endif
next
wait

It also demonstrates the reliability of frac()=0 to catch all the integer square roots :)
Title: Re: Is this a bug?
Post by: jack on September 10, 2020, 06:49:18 AM
I confirm that the exe that Nicola posted produces the problem
@Nicola
does the problem also happen when you "Run program directly" ?
Title: Re: Is this a bug?
Post by: Charles Pegge on September 10, 2020, 09:19:46 AM
Yes, I get that too. How did you compile it, Nicola? I've tried all the methods I could think of, but was unable to reproduce the bug.
Title: Re: Is this a bug?
Post by: Nicola on September 10, 2020, 11:12:29 AM
@Jack
Yes, even when it runs directly.

@Charles
Reply #22 --> it is OK. FRAC () works fine, as I had already seen in my program. At this point I think that the problem comes from INPUT.
Also interesting is the use of "tab" and "cr" ... ;)

With Oxide. CTRL-F5 or CTRL-SHIFT-F5
I take o2 from github
Title: Re: Is this a bug?
Post by: JRS on September 10, 2020, 12:29:54 PM
OT

I tried to setup my Xfinity Flex stream box which is connected to an older LCD monitor without speakers. (have a sound bar) The setup program died after trying to optimize surroundasound for the HDMI signal.

Resolve was putting a newer monitor on it temporarily that had speakers to get through the setup.

Somehow this problem Nicola is having rings a similar bell.

Title: Re: Is this a bug?
Post by: Nicola on September 10, 2020, 12:57:52 PM
@John
... I didn't quite understand what you mean, explain yourself better ...
a) am I using an old thing on a new pc? ...
b) am I using a new thing on an old pc?
c) ....?
Title: Re: Is this a bug?
Post by: Charles Pegge on September 10, 2020, 01:29:48 PM
Nicola,
You can try eliminating the ltrim rtrim.
Also try removing input() completely and set a to a fixed value. (ctrl-c will terminate)
Title: Re: Is this a bug?
Post by: JRS on September 10, 2020, 03:13:04 PM
c) Something is missing with your environment?
Title: Re: Is this a bug?
Post by: Nicola on September 11, 2020, 12:44:31 AM
@Charles
Input is to be excluded. I tried as you suggested, the problem remains.

What is the difference in o2 progress?
Title: Re: Is this a bug?
Post by: Charles Pegge on September 11, 2020, 01:23:20 AM
Thanks Nicola, that excludes Input().

Something in your 'Windows 10 Enterprise' system alters the behaviour of the compiler. Could you try fiddling with the printl statement:

    print "a       = " a cr
    print "a       = " str(a) cr
    etc.

OxygenBasicProgress is the most recent release
Title: Re: Is this a bug?
Post by: JRS on September 11, 2020, 01:24:47 AM
I liked clicking on the Wizard and getting the latest release. A bit confusing what to grab on Github.

Charles,

I have Windows 10 Pro if you would like me to give that a try.
Title: Re: Is this a bug?
Post by: Nicola on September 11, 2020, 03:07:50 AM
@Charles
Well, I solved everything with this change:

Code: [Select]
dim a, b as string
... But, strange to say, if I do
Code: [Select]
if a = "0" then exit do   
it doesn't work. Why?
Title: Re: Is this a bug?
Post by: Charles Pegge on September 11, 2020, 03:43:20 AM
Thanks Nicola,

This is a string comparison: if a="" then exit do.
We also have a boolean null and empty string interpretation if not a then exit do'

But I still wonder whether str(a) works with a as double.



Yes John, a test on your system would be very useful.

About 6 rounds
Code: [Select]
    include "$/inc/console.inc"
     
    dim a,b as double
    global r as int
    sub _print()
    ====================================
            if r=1 then
                    printl "YES is an exact root"
            else
                    printl "NO it is not an exact root"
            end if
            printl
            r=0
    end sub
     
     
    '====== MAIN ===============
    do
     
    r=0
    printl "input a number: "
    'a=input()
    a = ltrim rtrim Input ' per togliere caratteri da input tipo CR
    if a=0 then exit do
cls
    b = sqrt(a)
    print "a       = " a cr
    print "SQRT(a) = " b cr
     
    printl "Text if the result = INT(result)"
    if b=int(b) then
             r=1
    end if
    _print
     
    printl "Text if the FRAC(result) has a fractional component"
    if frac(b)=0 then
            r=1
    end if
    _print
     
    printl "Text if result = TRUNC(result)"
    if b=trunc(b) then
            r=1
    end if
    _print
     
    printl "Test if Result - INT(result) = 0"
    if b - int(b) = 0 then
            r=1
    end if
    _print
     
    printl "Test if INT(Result)^2 = number in input"
    if int(b)*int(b) = a then
            r=1
    end if
    _print
     
    loop
Title: Re: Is this a bug?
Post by: JRS on September 11, 2020, 04:51:27 AM
Here is the results on my Windows 10 Pro laptop. (up to date)

Code: [Select]
a       = 25
SQRT(a) = 5

Text if the result = INT(result)
NO it is not an exact root

Text if the FRAC(result) has a fractional component
YES is an exact root

Text if result = TRUNC(result)
YES is an exact root

Test if Result - INT(result) = 0
YES is an exact root

Test if INT(Result)^2 = number in input
YES is an exact root

--------------------------------------------------
a       = 63
SQRT(a) = 7.9372539331937721

Text if the result = INT(result)
NO it is not an exact root

Text if the FRAC(result) has a fractional component
NO it is not an exact root

Text if result = TRUNC(result)
NO it is not an exact root

Test if Result - INT(result) = 0
NO it is not an exact root

Test if INT(Result)^2 = number in input
YES is an exact root

--------------------------------------------------
a       = 33
SQRT(a) = 5.7445626465380286

Text if the result = INT(result)
NO it is not an exact root

Text if the FRAC(result) has a fractional component
NO it is not an exact root

Text if result = TRUNC(result)
NO it is not an exact root

Test if Result - INT(result) = 0
NO it is not an exact root

Test if INT(Result)^2 = number in input
NO it is not an exact root

--------------------------------------------------
a       = 50
SQRT(a) = 7.0710678118654755

Text if the result = INT(result)
NO it is not an exact root

Text if the FRAC(result) has a fractional component
NO it is not an exact root

Text if result = TRUNC(result)
NO it is not an exact root

Test if Result - INT(result) = 0
NO it is not an exact root

Test if INT(Result)^2 = number in input
NO it is not an exact root

--------------------------------------------------
a       = 128
SQRT(a) = 11.313708498984761

Text if the result = INT(result)
NO it is not an exact root

Text if the FRAC(result) has a fractional component
NO it is not an exact root

Text if result = TRUNC(result)
NO it is not an exact root

Test if Result - INT(result) = 0
NO it is not an exact root

Test if INT(Result)^2 = number in input
NO it is not an exact root

--------------------------------------------------
a       = 3
SQRT(a) = 1.7320508075688772

Text if the result = INT(result)
NO it is not an exact root

Text if the FRAC(result) has a fractional component
NO it is not an exact root

Text if result = TRUNC(result)
NO it is not an exact root

Test if Result - INT(result) = 0
NO it is not an exact root

Test if INT(Result)^2 = number in input
YES is an exact root

Note: Works from the IDE and as an .exe.
Title: Re: Is this a bug?
Post by: Nicola on September 11, 2020, 04:57:27 AM
Thanks Charles.
Anyway, I was thinking, that if A is a string and I insert 0 (zero) in the INPUT, the condition
IF A = ​​"0"
must be true, otherwise there is a problem. Do not you agree?

The solutions you mentioned are good, but they should not rule out the possibility of taking the test I indicated.

-- Could it be possible that my version of Window will output a compile that produces this error?
Title: Re: Is this a bug?
Post by: Charles Pegge on September 11, 2020, 05:40:59 AM
Yes, I was thinking of empty inputs.

A test to see what is going on with string a:

After cls
Code: [Select]
int i
for i=1 to len(a)
  print  ">>" i tab asc(a,i) cr
next

Title: Re: Is this a bug?
Post by: Nicola on September 11, 2020, 06:29:20 AM
Something actually happens after sixth input, or turn. I have attached the results. The test_1 matches the results by inserting the control of every single asci, and strangely also the print a is always ok. The test_2 is without the check you indicated, and again the strange characters come out.

Title: Re: Is this a bug?
Post by: Charles Pegge on September 11, 2020, 07:20:25 AM
Thanks Nicola,
Could you try running test_1 again but this time remove ltrim rtrim, so 'a' gets the whole input string. What happens on the 6th round is really curious.
Title: Re: Is this a bug?
Post by: Nicola on September 11, 2020, 08:32:01 AM
the test I did was already without ltrim rtrim
 :)
Title: Re: Is this a bug?
Post by: Charles Pegge on September 11, 2020, 09:55:56 AM
Can we find out what your console mode is:

Code: [Select]
int i
GetConsoleMode(ConsIn,i)
print "Cons input mode: " hex (i) cr
    print "a       = " a cr
    print "SQRT(a) = " b cr

mine is 1B7

If yours is different then you could try setting it before the do block
Code: [Select]
SetConsoleMode(ConsIn,0x1B7)

Title: Re: Is this a bug?
Post by: JRS on September 11, 2020, 11:11:53 AM
Any chance you have WSL (Windows Subsystem for Linux) installed?

Quote
Under Microsoft Windows [Version 10.0.15063], attempting to use the WriteConsoleW low-level API to output to the console results in no text being printed under WSL.
Title: Re: Is this a bug?
Post by: Nicola on September 12, 2020, 07:45:59 AM
Hi Charles.
First of all I wanted to tell you that these commands on the console are interesting.
I did the test by inserting the changes (the program is attached).
Nothing has changed. Although, I repeat, running the for ... next to see the asci the strange characters do not come out ...
Hello
Title: Re: Is this a bug?
Post by: Charles Pegge on September 12, 2020, 12:18:28 PM
Nicola,

At last! This line causes the problem:

Code: [Select]
if b=int(b) then r=1 : end if

int(n) is currently not legit syntax
int is only to be used as a type.

The next release of o2 will catch this error.
Title: Re: Is this a bug?
Post by: JRS on September 12, 2020, 02:00:55 PM
In most BASIC languages INT() is a function to return a integer number. SB has INT and FIX to give you more granularity on how it rounds. (up / down)

Could INT be a cast and achive the same result?

Code: OxygenBasic
  1. float f=4.25
  2. sys a
  3. a=(int) f
  4. print a
  5.  

I'm spoiled as SB is a typleless scripting language and you don't have to DECLARE anything including arrays.
Title: Re: Is this a bug?
Post by: JRS on September 13, 2020, 11:45:19 AM
Here is a remake with INT as a cast.

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


a       = 25
SQRT(a) = 5

Text if the result = (INT) result
NO it is not an exact root

Text if the FRAC(result) has a fractional component
YES is an exact root

Text if result = TRUNC(result)
YES is an exact root

Test if Result - (INT) result = 0
NO it is not an exact root

Test if (INT) Result ^ 2 = number in input
NO it is not an exact root

---------------------------------------------------

a       = 3
SQRT(a) = 1.7320508075688772

Text if the result = (INT) result
NO it is not an exact root

Text if the FRAC(result) has a fractional component
NO it is not an exact root

Text if result = TRUNC(result)
NO it is not an exact root

Test if Result - (INT) result = 0
NO it is not an exact root

Test if (INT) Result ^ 2 = number in input
NO it is not an exact root

Title: Re: Is this a bug?
Post by: Charles Pegge on September 13, 2020, 05:33:41 PM
Hi John,

Yes you can cast (int) but the effect is to treat the floating point binary as integer binary. They have different encodings

But you can use convert

Code: [Select]
uses console
double g=5/3
print "g ="     tab g cr
print "cast"    tab str((int) g) cr
print "cast"    tab str(cast int g) cr
print "convert" tab str(convert int g) cr
wait
Title: Re: Is this a bug?
Post by: JRS on September 13, 2020, 06:17:19 PM

g =     1.6666666666666667
cast    -1431655765
cast    -1431655765
convert 2


Thanks for clearing up what a cast int is. The convert is a handy keyword.



Title: Re: Is this a bug?
Post by: JRS on September 13, 2020, 06:48:33 PM
Here is a remake using convert int.

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


a       = 25
SQRT(a) = 5

Text if the result = convert int result
NO it is not an exact root

Text if the FRAC(result) has a fractional component
YES is an exact root

Text if result = TRUNC(result)
YES is an exact root

Test if Result - convert int result = 0
YES is an exact root

Test if convert int Result ^ 2 = number in input
YES is an exact root

Title: Re: Is this a bug?
Post by: Nicola on September 14, 2020, 12:42:21 AM
Hi Charles.
However apparently INT () works correctly. In fact, by definition, INT returns the nearest integer. For example INT (3.49) should equal 3, while INT (3.51) should equal 4.
The test I made proves it (see the attached txt file).

Code: OxygenBasic
  1. use console
  2.  
  3. cls
  4.  
  5. double i
  6. for i=1 to 15
  7.   printl i tab sqrt(i) tab int(sqrt(i))
  8. next
  9. waitkey
  10.  

I did research and found that CAST and CONVERT are used in SQL ...
Is there a place in O2 where they are explained?
Title: Re: Is this a bug?
Post by: Charles Pegge on September 14, 2020, 02:19:44 AM
Hi Nicola,

The problems arise when putting int() into a conditional expression. I will try to improve the situation but I strongly recommend using frac or round since these enable float-float comparisions directly on the Floating point processor (FPU).

One possible solution is for o2 to intercept int() and turn it into round() :)

frac translates to one assembler instruction frac, and round translates to frndint. This is much faster than integer-float conversions.

The cast and convert keywords are briefly described in the manual.

Title: Re: Is this a bug?
Post by: Nicola on September 14, 2020, 05:03:03 AM
Thanks Charlie, also for your patience.
At this point I like to see the results I get with the set of functions you have indicated to me.
For printing convenience I put the _str function. I'm sure you already have some function in o2 that does the same thing ...
Code: OxygenBasic
  1. use console
  2.  
  3. function _str(string s,int d=20) as string
  4.         function = s+ space(d-len(s))
  5. end function
  6.  
  7. cls
  8.  
  9. double N,r
  10. print "N" tab _str("sqrt(N)",22) _str("frac(sqrt(N))") _str("trunc(sqrt(N))") _str("ceil(sqrt(N))") _str("floor(sqrt(N))") cr
  11. for N=1 to 16
  12.   r=sqrt(N)
  13.   print N tab _str(r,22) _str(frac(r)) _str(trunc(r)) _str(ceil(r)) _str(floor(r)) cr
  14. next
  15. waitkey
  16.  

This is the result:

N       sqrt(N)               frac(sqrt(N))       trunc(sqrt(N))      ceil(sqrt(N))       floor(sqrt(N))
1       1                     0                   1                   1                   1
2       1.4142135623730951    0.4142135623730951  1                   2                   1
3       1.7320508075688772    0.7320508075688772  1                   2                   1
4       2                     0                   2                   2                   2
5       2.2360679774997898    0.2360679774997898  2                   3                   2
6       2.4494897427831779    0.4494897427831779  2                   3                   2
7       2.6457513110645907    0.6457513110645907  2                   3                   2
8       2.8284271247461903    0.8284271247461903  2                   3                   2
9       3                     0                   3                   3                   3
10      3.1622776601683795    0.1622776601683795  3                   4                   3
11      3.3166247903553998    0.3166247903553998  3                   4                   3
12      3.4641016151377544    0.4641016151377544  3                   4                   3
13      3.6055512754639891    0.6055512754639891  3                   4                   3
14      3.7416573867739413    0.7416573867739413  3                   4                   3
15      3.872983346207417     0.872983346207417   3                   4                   3
16      4                     0                   4                   4                   4


Hello
Title: Re: Is this a bug?
Post by: Charles Pegge on September 14, 2020, 07:43:53 AM
You can limit the decimal places by using a second param for str():

example:
str(1/3, 5)
0.33333

Using 0 will give you rounded integer strings.
Title: Re: Is this a bug?
Post by: Nicola on September 14, 2020, 11:03:23 AM
Great!
But what if you need some extra space?
Title: Re: Is this a bug?
Post by: JRS on September 14, 2020, 03:17:19 PM
Does O2 have a PRINT FORMAT like function?
Title: Re: Is this a bug?
Post by: Charles Pegge on September 14, 2020, 03:50:44 PM
We can use sprintf from msvcrt.dll.

But here is a function to support decimal-point alignment in a column, taking care of left and right spacing.

Code: [Select]
function dp_str(string num="", int wid=20, dpp=10) as string
===========================================================
  'num  number string
  'wid  column width
  'dpp  decimal point position in column
  '
  string buf 'column buffer
  int ln     'length of num
  int dp     'offset of num decimal point
  '
  buf=space(wid) ' column string
  ln=len(num)
  dp=instr(num,".")
  if dp=0 'integers
    dp=ln+1
  endif
  '
  int n=dpp-dp 'left spaces
  'check if too long
  if n<0 then n=0 'remove left space
  if ln>wid
    mid buf,1,"!!"+num 'error
  else
    mid(buf,1+n,num) 'copy number into position
  endif
  return buf
end function

'TESTS
uses console
print dp_str(pi(),30)      "|" cr
print dp_str(pi(),30,4)    "|" cr
print dp_str(str(pi(),5))  "|" cr
print dp_str(12345678)     "|" cr
print dp_str(123)          "|" cr
print dp_str(-123)         "|" cr
print dp_str(-123.45)      "|" cr
wait
Title: Re: Is this a bug?
Post by: JRS on September 14, 2020, 04:48:26 PM
Charles,

It might be worth translating ScriptBasic's PRINT FORMAT function from C to O2. A function I couldn't live without.

FORMAT (https://www.scriptbasic.org/docs/ug/ug_25.64.html)

Not sure how O2 would deal with a varidic like argument passing.
Title: Re: Is this a bug?
Post by: Charles Pegge on September 15, 2020, 01:53:02 AM
Hi John,

I see that SB Format is based on sprintf.

This is a useful reference:

http://www.cplusplus.com/reference/cstdio/printf/

Variadics are supported for all msvcrt functions that use them.

Example:
Code: [Select]
uses corewin
uses console
char buf[100]
double d=pi()
char buf[100]
double d=pi()

'sprintf_s(buf,100,"floats: %4.2f %+.0e %E " cr, d, d, d)
'sprintf(buf, "floats: %4.2f %+.0e %E " cr, d, d, d)

   sprintf (buf, "Characters: %c %c ", asc("a"), 65)
   print buf cr
   sprintf (buf, "Decimals: %d %ld", 1977, 650000L)
   print buf cr
   sprintf (buf, "Preceding with blanks: %10d ", 1977)
   print buf cr
   sprintf (buf, "Preceding with zeros: %010d ", 1977)
   print buf cr
   sprintf (buf, "Some different radices: %d %x %o %#x %#o ", 100, 100, 100, 100, 100)
   print buf cr
   sprintf (buf, "floats: %4.2f %+.0e %E", double 3.1416, double 3.1416, double 3.1416)
   print buf cr
   sprintf (buf, "Width trick: %*d", 5, 10)
   print buf cr
   sprintf (buf, "%s", "A string")
   print buf cr
   print cr
wait
Title: Re: Is this a bug?
Post by: JRS on September 15, 2020, 03:38:54 AM
That's close enough for me. I do use the custom SB number formatting string a lot as well. It looks like it wouldn't be too hard to create a O2 FORMAT function using sprintf and a bit of extra code to handle the numeric formatting.

Code: Script BASIC
  1. PRINT FORMAT("Line Number: %~0000~ Square root of 5 is %.3f\n", 25, SQR(5))


C:\ScriptBASIC\examples>sbc format.sb
Line Number: 0025 Square root of 5 is 2.236

C:\ScriptBASIC\examples>



The FORMAT function is in string.c SB source.
Title: Re: Is this a bug?
Post by: Nicola on September 16, 2020, 02:46:41 AM
@Charles
 Reply #58

Great!
Is this good for both console and gui edit-text?
Title: Re: Is this a bug?
Post by: JRS on September 16, 2020, 03:06:21 AM
sprintf is generating a formatted string that can be used in any UI environment.
Title: Re: Is this a bug?
Post by: Charles Pegge on September 16, 2020, 04:21:22 AM
Yes Nicola,

But with GUI edit boxes showing aligned data, you will need to set a monospaced typeface like "Courier" or "Lucida Console"

Code: [Select]
...
  function SetFont(sys *hwnd,n,height,width,weight,string fontname) as sys
  ========================================================================
  indexbase 1
  sys hFont,i
  hfont=CreateFont( height,width,0,0,weight,0,0,0,0,0,0,0,0,fontname)
  int i
  for i=1 to n
    SendMessage hwnd(i),WM_SETFONT,hfont,0
  next
  return hFont
  end function
...

    hFont=SetFont(hchw[1],6,13,9,400,"Lucida console")

Title: Re: Is this a bug?
Post by: JRS on September 16, 2020, 06:41:00 AM
I tend to use grid controls for formatted columns of data in a GUI environment using the controls alignment properties to do the work.