QB64 Phoenix Edition
I also have a question - about DefInt - 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: I also have a question - about DefInt (/showthread.php?tid=2957)



I also have a question - about DefInt - PhilOfPerth - 08-16-2024

While youse guys are studying cause and effect, I've been experimenting with the DefInt function, and I don't understand the results.
The DefInt function truncates all variables of that name to their integer values, and they then operate with this integer value, right?
But why do I get the Inf result in this snippet? I guess it's to do with their binary expression? 
t1 and t2 are "simple" integers aren't they?
Code: (Select All)
DefInt T
t1 = Timer
Print "T1 is integer. ", , , t1
Sleep 2
ActOnT


Sub ActOnT
    t2 = Timer
    Print "t2 is integer in sub. ", , , t2
    t3 = t1 / t2
    Print "t3 is result of t1 divided by t2, I expect 0. ", t1 / t2
    t4 = t2 / t1
    Print "t4 is result of t2 divided by t1, I expect 1. ", t2 / t1
End Sub



RE: I also have a question - about DefInt - SMcNeill - 08-16-2024

Division by 0.

T1 isn't a shared variable.


RE: I also have a question - about DefInt - DSMan195276 - 08-16-2024

You're seeing the difference between `/` division and `\` division. Basically, "regular" division turns both arguments into floating-point (`DOUBLE`) values before doing the division, and the result is another floating-point value. If you pass the expression directly to `Print` then you'll be printing the resulting `DOUBLE` value.

If you instead do `t4 = t2 / t1`separately and pass `t4` to `Print` then you'll always see an integer. The assignment of the value to `t4` turns the value back into an integer value. Alternatively, you can Print `t2 \ t1`, which will do integer division. Integer division does not turn either value into a floating-point value and will give you an integer result.

Separately you get Inf because you're doing a divide-by-zero, the `t1` variable is not shared with the `Sub`.


RE: I also have a question - about DefInt - Pete - 08-17-2024

DEFINT SHARED T Big Grin Big Grin Big Grin 

Get on it, developers!

Pete

- Sorry, channeling Clippy again.


RE: I also have a question - about DefInt - PhilOfPerth - 08-17-2024

(08-16-2024, 11:43 PM)SMcNeill Wrote: Division by 0.

T1 isn't a shared variable.

Of course!!! [Image: shocked.png]

(08-16-2024, 11:45 PM)DSMan195276 Wrote: You're seeing the difference between `/` division and `\` division. Basically, "regular" division turns both arguments into floating-point (`DOUBLE`) values before doing the division, and the result is another floating-point value. If you pass the expression directly to `Print` then you'll be printing the resulting `DOUBLE` value.

If you instead do `t4 = t2 / t1`separately and pass `t4` to `Print` then you'll always see an integer. The assignment of the value to `t4` turns the value back into an integer value. Alternatively, you can Print `t2 \ t1`, which will do integer division. Integer division does not turn either value into a floating-point value and will give you an integer result.

Separately you get Inf because you're doing a divide-by-zero, the `t1` variable is not shared with the `Sub`.

Ah, got it; thanks.