Is whole number?

[code]dim dRoot as double

dRoot = sqrt(13) // - which is 3.6055512754639892931192212674705, obviously not whole

if dRoot mod 1 = 0 then
msgbox “number is whole”
else
msgbox “number is not”
end if[/code]

if dRoot mod 1 check always returns 0 whether dRoot is as above (non-whole value), or dRoot is something like 4 (whole value).

Is this a bug, or am I using mod improperly?

You’re using it improperly. Anything mod 1 is zero. Use Floor() to extract the integer portion of the number.

if dRoot = floor(dRoot) then msgbox "number is whole" else msgbox "number is not" end if

Figured I had to have been… appreciate it, Tim.

another way, which might be faster (or not?) is to use integer division:

if (dRoot \\ 1) = dRoot then
  msgbox "whole"
else
  msgbox "not"
end if

I put both algorithms to the test and the integer division (\) method seems to be slightly faster.

Here are the algorithms and results

[code]Dim i as Integer
Dim result as Boolean
Dim dRoot As Double
Dim rnd As new Random

dRoot = rnd.InRange(1, 100) / 10

for i = 1 to 10000000
result = (dRoot = floor(dRoot))
next i
[/code]
TOTAL TIME = 1211 milliseconds

[code]Dim i as Integer
Dim result as Boolean
Dim dRoot As Double
Dim rnd As new Random

dRoot = rnd.InRange(1, 100) / 10

for i = 1 to 10000000
result = ((dRoot \ 1) = dRoot)
next i
[/code]
TOTAL TIME = 939 milliseconds

Thanks Alwyn - for a better test, please run it with #pragma disableBackgroundTasks as this will remove the yield check at the loop boundary, and give a better estimate of the time spent in the loop.

  #pragma disableBackgroundTasks
  
  Dim i as Integer
  Dim result as Boolean
  Dim dRoot As Double
  Dim rnd As new Random
  
  dRoot = rnd.InRange(1, 100) / 10
  
  for i = 1 to 10000000
    result = (dRoot = floor(dRoot))
  next i 

TOTAL TIME = 1054 milliseconds (12.96% speed improvement)

  #pragma disableBackgroundTasks
  
  Dim i as Integer
  Dim result as Boolean
  Dim dRoot As Double
  Dim rnd As new Random
  
  dRoot = rnd.InRange(1, 100) / 10
  
  for i = 1 to 10000000
    result = ((dRoot \\ 1) = dRoot)
  next i

TOTAL TIME = 829 milliseconds (11.71% speed improvement)

Hmmm, didn’t realize “disableBackgroundTasks” would make such a big speed difference. Thanks for the tip Michael.

You’re testing within the compiled app, not the debug version, right?

Actually, I was using the debug version. Here is the results when profiling the compiled app:

#pragma disableBackgroundTasks
...
for i = 1 to 10000000
  result = (dRoot = floor(dRoot))
next i 

TOTAL TIME = 520 milliseconds (50.66% faster than debug version)

#pragma disableBackgroundTasks
...
for i = 1 to 10000000
  result = ((dRoot \\ 1) = dRoot)
next i

TOTAL TIME = 215 milliseconds (74.07% faster than debug version)

That is some serious speed for a loop of 10 million iterations.

[quote=22815:@Alwyn Bester]
Hmmm, didn’t realize “disableBackgroundTasks” would make such a big speed difference. Thanks for the tip Michael.[/quote]

By default, Xojo checks to see whether it need to yield to other threads on every loop iteration - that’s a non-trivial amount of code, and as you’ve demonstrated, if the code inside the loop is tiny, there is move time spent in the loop housekeeping than in the code inside the loop.

My practice: I tend to put #pragma disableBackgroundTasks everywhere, and then as I fine-tune the app for performance I’ll add explicit Yields() in strategic places.

where do you usually put #pragma disableBackgroundTasks in??

As the first line (or so) of the method.

thanks paul.

I believe that #pragma disableBackgroundTasks only affects code that comes after it in the method, which is different than other pragmas such as #stackChecking which I think affects the entire method regardless of where it shows up (I may be wrong on this, don’t quote me)

i just try putting #pragma disableBackgroundTask in the beginning of method that i have for next loop (eg looping through all the controls in a form). Seem the application a little faster.

in what situation i can make use of this pragma #pragma StackChecking ?

Pragmas are “expert” features, so you should definitely read about them in the Language Reference and be cautious using them.

In my experience, the one that makes the most difference in performance is DisableBackgroundTasks. If you absolutely need every single bit of performance, you might also try DisableBoundsChecking, NilObjectChecking and StackOverflowChecking but these are dangerous as they turn off important error checking at runtime. Only use those with perfectly debugged code.

thanks