Sign in to follow this  
Followers 0
Kiti

Integer or not?

4 posts in this topic

Hello!

I've wrote this code, run it to see what it happens:

$ET = 4567
$LS = 20
$LR = 9
$TE = 12
$NS = 23
$p = 1


For $i = 0 To 10
 
    $x = ((Ceiling($ET / 1.1) * 1.1 - 1.1 * Int(20 * $LS * 1.1 ^ $LS) - 1.1 * Int($p * 30 * $LR * (1.05 + $TE * 0.01) ^ $LR))) / ($NS * 1.1)
 
    If IsInt($x) Then
    MsgBox(0, "","IF $p = " & $p & " then " & $x & " is integer")
    Else
    MsgBox(0, "","IF $p = " & $p & " then " & $x & " is NOT integer")
    EndIf
 
 
    $p = $p - 0.1
 
Next

When $p is 0.8 $x is 25 but is NOT recognized as an integer. The message box says "IF $p = 0.8 then 25 in NOT integer". Why?? 25 IS an integer! How can I solve this?

Thank you in advance!

Share this post


Link to post
Share on other sites



Its becouse its a floating point number. The best way I found was to test: "StringInStr($x, ".")" And if you want to test if its a specified number: 'String($x) = "25"'.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I don't know what number it will be, I just want to do something when it's an integer.

The StringInStr test is great, thank you very much!

And one more thing:

At the 10th run, when $p should be 0, it displays it as 1.38777878078145e-016.

Why is that?

Is there any nicer approach than "If $i = 10 then $p = 0" ?

Edited by Kiti

Share this post


Link to post
Share on other sites

I don't know what number it will be, I just want to do something when it's an integer.

The StringInStr test is great, thank you very much!

And one more thing:

At the 10th run, when $p should be 0, it displays it as 1.38777878078145e-016.

Why is that?

Is there any nicer approach than "If $i = 10 then $p = 0" ?

This method should work for all numbers 

$ET = 4567
$LS = 20
$LR = 9
$TE = 12
$NS = 23
$p = 1


For $i = 0 To 10

    $x = ((Ceiling($ET / 1.1) * 1.1 - 1.1 * Int(20 * $LS * 1.1 ^ $LS) - 1.1 * Int($p * 30 * $LR * (1.05 + $TE * 0.01) ^ $LR))) / ($NS * 1.1)
    If _ValueIsInt($x) Then
            MsgBox(0, "","IF $p = " & $p & " then " & $x & " is integer")
    Else
            MsgBox(0, "","IF $p = " & $p & " then " & $x & " is NOT integer")
    EndIf


    $p = $p - 0.1

Next

Func _ValueIsInt($val)
    If (($val - Int($val)) < 0.00000000000001) Then
            Return True
        Else
            Return False
        EndIf
EndFunc

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0