user1137890
user1137890

Reputation: 157

VBS script return expected result, how to compare float value?

I am getting an interesting result when executing the following VB script.

Set StdOut = WScript.StdOut
Set wbemSvc = GetObject("winmgmts://" & "." & "/root/cimv2")
Set biosSet = wbemSvc.ExecQuery("Select * from Win32_BIOS")

For Each biosObj In biosSet
  StdOut.WriteLine "SMBIOSMajorVersion=" & biosObj.SMBIOSMajorVersion
  StdOut.WriteLine "SMBIOSMinorVersion=" & biosObj.SMBIOSMinorVersion
Next

  StdOut.WriteLine "Return value is: " & IsNewBiosVersion

Function IsNewBiosVersion()

  On Error Resume Next
  Set biosSet = wbemSvc.ExecQuery("Select * from Win32_BIOS")

  newBios = 0
  For Each bios In biosSet
    minorFloat = "." & bios.SMBIOSMinorVersion
    If bios.SMBIOSMajorVersion > 2 OR (bios.SMBIOSMajorVersion = 2 AND minorFloat >= .6) Then
      newBios = 1
    End If
  Next
  IsNewBiosVersion = newBios

End Function

The result is as follow. This looks contradictory since the SMBIOSMinorVersion=4, according to the code logic in the script, the return value should be 0!!!

SMBIOSMajorVersion=2
SMBIOSMinorVersion=4
Return value is: 1

I ran this same script on another system and got the expected correct result.

SMBIOSMajorVersion=2
SMBIOSMinorVersion=4
Return value is: 0

So what is the problem here?

New update:

We execute the following script again on the system, and found that the CDbl() function does not convert the string "2.4" to double value correctly, instead it converts it to 24! Looks like the dot "." was lost when converting, what is wrong with this? An bug in CDbl or a violation when use it?

here is the script

Set StdOut = WScript.StdOut
StdOut.WriteLine ""
StdOut.WriteLine "Simple Function to Test BIOS Version"
StdOut.WriteLine ""
Set wbemSvc = GetObject("winmgmts://" & "." & "/root/cimv2")
Set biosSet = wbemSvc.ExecQuery("Select * from Win32_BIOS")

For Each bios In biosSet
  newBios = 0
  StdOut.WriteLine "SMBIOSMajorVersion=" & bios.SMBIOSMajorVersion
  StdOut.WriteLine "SMBIOSMinorVersion=" & bios.SMBIOSMinorVersion
  temp = bios.SMBIOSMajorVersion & "." & bios.SMBIOSMinorVersion
  StdOut.WriteLine "major dot minor=" & temp

  currentBios = CDbl(bios.SMBIOSMajorVersion & "." & bios.SMBIOSMinorVersion)
  StdOut.WriteLine ""
  StdOut.WriteLine "currentBios=" & currentBios
  If currentBios >= 2.6 Then newBios = 1
  StdOut.WriteLine "return value is: " & newBios
Next

here is the output

Simple Function to Test BIOS Version

SMBIOSMajorVersion=2
SMBIOSMinorVersion=4
major dot minor=2.4

currentBios=24
return value is: 1

Upvotes: 0

Views: 844

Answers (2)

Bond
Bond

Reputation: 16311

Why not just convert the major.minor values to floating-point for your test? You're currently doing two separate tests, with one being a string vs float comparison, which is unusual.

Maybe try this instead?

currentBios = CDbl(bios.SMBIOSMajorVersion & "." & bios.SMBIOSMinorVersion)

If currentBios >= 2.6 Then newBios = 1

You have to be careful comparing floating-point values when math operations are involved but for literal values you'll be fine.

And, as already mentioned, remove On Error Resume Next or you may never know why it works on one PC but not another.

Upvotes: 1

Sam Makin
Sam Makin

Reputation: 1556

Remove your error handling - it s probably suppressing an issue.

On Error Resume Next ' Not a good idea

For one thing you are comparing a string to a double in these lines:

minorFloat = "." & bios.SMBIOSMinorVersion
If bios.SMBIOSMajorVersion > 2 OR (bios.SMBIOSMajorVersion = 2 AND minorFloat >= .6) Then

Upvotes: 2

Related Questions