### Author Topic: Unexpected behavior with floating point values  (Read 1137 times)

0 Members and 1 Guest are viewing this topic.

#### Ed Davis

• Newbie
• Posts: 12
##### Unexpected behavior with floating point values
« on: March 12, 2016, 05:48:35 PM »
I'm not sure if this is a problem, or if I'm just doing something wrong.  See this simple code snippet:
Code: [Select]
`float a, b, x, y, j, ka = 0b = -1x = (a <> 0)    ' (1) expect 0  got 0y = (b <> 0)    ' (2) expect -1 got -1j = (x and y)   ' (3) expect 0  got 0k = (a and b)   ' (4) expect 0  got 0print "x: " x " y: " y " j: " j " k: " ka = 0b = -1x = (a and b)                                   ' (5) expect 0 got: 0y = (a <> 0) and (b <> 0)                       ' (6) expect 0 got: #qNANif (a and b) then j = -1 else j = 0             ' (7) expect 0 got: -1if (a <> 0) and (b <> 0) then k = -1 else k = 0 ' (8) expect 0 got: 0print "x: " x " y: " y " j: " j " k: " k`
When the first print statement is performed, the values are what I expected, as noted on items 1 - 4.

However, the next set of expressions give me a couple of unexpected values - specifically expressions (6) and (7).  Can someone explain why these two don't match the others?  Based on 1-4 and 5 and 8, I expected different values for (6) and (7).

Thanks!

#### jack

• Jr. Member
• Posts: 86
##### Re: Unexpected behavior with floating point values
« Reply #1 on: March 12, 2016, 07:27:56 PM »
hi Ed Davis
I had to modify your code a bit for it to compile with the latest version
Code: [Select]
`_define a, b, x, y, j, k as _floata = 0b = -1x = (a <> 0)    ' (1) expect 0  got 0y = (b <> 0)    ' (2) expect -1 got -1j = (x and y)   ' (3) expect 0  got 0k = (a and b)   ' (4) expect 0  got 0print "x: " x " y: " y " j: " j " k: " ka = 0b = -1x = (a and b)                                   ' (5) expect 0 got: 0y = (a <> 0) and (b <> 0)                       ' (6) expect 0 got: #qNANif (a and b) then j = -1 else j = 0             ' (7) expect 0 got: -1if (a <> 0) and (b <> 0) then k = -1 else k = 0 ' (8) expect 0 got: 0print "x: " x " y: " y " j: " j " k: " k`the output
Quote
x: 0 y: -1 j: 0 k: 0
x: 0 y:  0 j: 0 k: 0

#### Ed Davis

• Newbie
• Posts: 12
##### Re: Unexpected behavior with floating point values
« Reply #2 on: March 13, 2016, 04:29:18 AM »
hi Ed Davis
I had to modify your code a bit for it to compile with the latest version
Code: [Select]
`_define a, b, x, y, j, k as _float..`the output
Quote
x: 0 y: -1 j: 0 k: 0
x: 0 y:  0 j: 0 k: 0

Hmmm.  What is the date of the version you are using?  I just downloaded (clicking on the wizard in the upper right corner), and the date of gxo2.exe is 06 Jul 2015.  With that version, what I posted runs unchanged.

When I run yours, it gives me an error:  it doesn't know what "_define" is.

Where do you get the latest version?   ..... (later) --- now I see the alpha downloads.  Is that what you are using?   Must not be, because the .exe has the same date.  Hmmm.

Thanks for the reply!

#### jack

• Jr. Member
• Posts: 86
##### Re: Unexpected behavior with floating point values
« Reply #3 on: March 13, 2016, 06:24:53 AM »
hello Ed Davis
I am very embarrassed, I must have had too much wine as I thought I was on a different forum and gave an answer pertaining to some other Basic.
my apologies

#### Arnold

• Hero Member
• Posts: 599
##### Re: Unexpected behavior with floating point values
« Reply #4 on: March 13, 2016, 09:28:47 AM »
Hi Ed,

if I use int instead of float I will get the expected results.
I am not sure if short circuit logic with floats will work in Oxygenbasic. I used two temporary vars y1 and y2 for (a <> 0) and (b <> 0) and as there is x = (a and b) I used this statement: if x then j = -1 else j = 0.
With these changes I would get the expected results too.

Roland

Code: [Select]
`int a, b, x, y, j, ka = 0b = -1x = (a <> 0)    ' (1) expect 0  got 0y = (b <> 0)    ' (2) expect -1 got -1j = (x and y)   ' (3) expect 0  got 0k = (a and b)   ' (4) expect 0  got 0print "x: " x " y: " y " j: " j " k: " ka = 0b = -1x = (a and b)                                   ' (5) expect 0 got: 0y = (a <> 0) and (b <> 0)                       ' (6) expect 0 got: 0if (a and b) then j = -1 else j = 0             ' (7) expect 0 got: 0if (a <> 0) and (b <> 0) then k = -1 else k = 0 ' (8) expect 0 got: 0print "x: " x " y: " y " j: " j " k: " k'------------------------------------------float a, b, x, y, j, k, y1, y2a = 0b = -1x = (a <> 0)    ' (1) expect 0  got 0y = (b <> 0)    ' (2) expect -1 got -1j = (x and y)   ' (3) expect 0  got 0k = (a and b)   ' (4) expect 0  got 0print "x: " x " y: " y " j: " j " k: " ka = 0b = -1x = (a and b)                                   ' (5) expect 0 got: 0y1 = (a <> 0) : y2 = (b <> 0) y = (y1 and y2)                                 ' (6) expect 0 got: 0if x then j = -1 else j = 0                     ' (7) expect 0 got: 0if (a <> 0) and (b <> 0) then k = -1 else k = 0 ' (8) expect 0 got: 0print "x: " x " y: " y " j: " j " k: " k`

#### Charles Pegge

• Admin Support Member
• Posts: 3584
##### Re: Unexpected behavior with floating point values
« Reply #5 on: March 16, 2016, 12:47:10 AM »
Doing logic with floats is tortuous and not recommended. However, I will clean up OxygenBasic's handling of this situation in the next release. I propose that floats should use only boolean logic, not bitwise logic. False will be zero, and all other states wil be treated as True. The outcome of a logical operation between floats will be false=0,  true=-1.