Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    49
  • comments
    86
  • views
    34411

Now for Code and a Rant

Sign in to follow this  
dwarfsoft

399 views

So, what exactly is with VBScript and its useless implementation? Why can't I have integers larger than 32Bit, or more to the point, why can't they be 64bit at LEAST?!

So, I know that some people are going to ask why I am using VBScript at all. VBScript happens to make a lot of sense simply for the way it can be used to interact with Active Directory on Domain Controllers, and the simplicity of making WMI calls. Plus, VBScript is so easy to start using and has a lot more immediate use than programming, and a wider feature set than Batch.

So I come to start developing a script that is checking for changes in drive sizes of Dynamic Disks and possible changes to the configuration of Extents within Partitions, and I can't compare two numbers returned from WMI or the DiskExt.exe program (do a search on the net, the most USEFUL thing I found in regards to disk extents for Dynamic Disks).

So I basically had to write my own string math functions (Addition and subtraction).

So here is that code, available for those who want it, those who need it, and those who are interested in it.

Function StringAddition(num1in,num2in)
' Cleanup
num1 = Trim(num1in)
num2 = Trim(num2in)

if num1 = "" and num2 = "" then
WScript.Quit
elseif num1 = "" then
StringAddition = num2
elseif num2 = "" then
StringAddition = num1
end if

if Asc(Mid(num1,Len(num1),1)) = 13 then
num1 = Mid(num1,1,Len(num1)-1)
end if
if Asc(Mid(num2,Len(num2),1)) = 13 then
num2 = Mid(num2,1,Len(num2)-1)
end if
if Asc(Mid(num1,Len(num1),1)) = 10 then
num1 = Mid(num1,1,Len(num1)-1)
end if
if Asc(Mid(num2,Len(num2),1)) = 10 then
num2 = Mid(num2,1,Len(num2)-1)
end if
if Asc(Mid(num1,Len(num1),1)) = 13 then
num1 = Mid(num1,1,Len(num1)-1)
end if
if Asc(Mid(num2,Len(num2),1)) = 13 then
num2 = Mid(num2,1,Len(num2)-1)
end if

if num1 = "" and num2 = "" then
WScript.Quit
elseif num1 = "" then
StringAddition = num2
elseif num2 = "" then
StringAddition = num1
end if

'fix an errar
if Len(num2) > len(num1) then
num1 = "0" & num1
end if
if Len(num1) > len(num2) then
num2 = "0" & num2
end if


j = 1
p = Int(Right(num1,j))
q = Int(Right(num2,j))
result = p+q
carry = Int(result) \ 10
result = Int(Int(result) mod 10)
if Carry <> 0 then
num1 = StringAddition(Mid(num1,1,Len(num1)-1), carry) & result
end if
if Len(num1) > 1 Then
StringAddition = TrimLeadingZeros(StringAddition(Mid(num1,1,Len(num1)-1),Mid(num2,1,Len(num2)-1)) & result)
else
StringAddition = TrimLeadingZeros(carry & result)
end if
End Function

Function StringSubtraction(num1in,num2in)
' Cleanup
num1 = Trim(num1in)
num2 = Trim(num2in)

if num1 = "" and num2 = "" then
WScript.Quit
elseif num1 = "" then
StringSubtraction = num2
Exit Function
elseif num2 = "" then
StringSubtraction = num1
Exit Function
end if

'the following fixes newlines in the input
if Asc(Mid(num1,Len(num1),1)) = 13 then
num1 = Mid(num1,1,Len(num1)-1)
end if
if Asc(Mid(num2,Len(num2),1)) = 13 then
num2 = Mid(num2,1,Len(num2)-1)
end if
if Asc(Mid(num1,Len(num1),1)) = 10 then
num1 = Mid(num1,1,Len(num1)-1)
end if
if Asc(Mid(num2,Len(num2),1)) = 10 then
num2 = Mid(num2,1,Len(num2)-1)
end if
if Asc(Mid(num1,Len(num1),1)) = 13 then
num1 = Mid(num1,1,Len(num1)-1)
end if
if Asc(Mid(num2,Len(num2),1)) = 13 then
num2 = Mid(num2,1,Len(num2)-1)
end if

if num1 = "" and num2 = "" then
WScript.Quit
elseif num1 = "" then
StringSubtraction = num2
ExitFunction
elseif num2 = "" then
StringSubtraction = num1
ExitFunction
end if

If Mid(num1,1,1) = "-" and Mid(num2,1,1) = "-" Then
StringSubtraction = StringSubtraction(Mid(num2,2,Len(num2)-1),Mid(num1,2,Len(num1)-1))
Exit Function
ElseIf Mid(num1,1,1) = "-" Then
StringSubtraction = "-" & StringAddition(num2,Mid(num1,2,Len(num1)-1))
Exit Function
Elseif Mid(num2,1,1) = "-" Then
StringSubtraction = StringAddition(Mid(num2,2,Len(num2)-1),num1)
Exit Function
End If

wib = WhichIsBigger(num1,num2)
if wib = 2 then
StringSubtraction = "-" & StringSubtraction(num2,num1)
Exit Function
end if

'fix an errar
if Len(num2) > len(num1) then
num1 = "0" & num1
end if
if Len(num1) > len(num2) then
num2 = "0" & num2
end if




j = 1
p = Int(Right(num1,j))
q = Int(Right(num2,j))
result = p-q
if (result < 0) then 'and (wib <> 2) then
carry = 1
result = result + 10
end if
result = Int(Int(result) mod 10)
if Carry <> 0 then
num1 = StringSubtraction(Mid(num1,1,Len(num1)-1), Abs(carry)) & Abs(result)
end if


if Len(num1) > 1 Then
StringSubtraction = TrimLeadingZeros(StringSubtraction(Mid(num1,1,Len(num1)-1),Mid(num2,1,Len(num2)-1)) & Abs(result))
else
StringSubtraction = TrimLeadingZeros(Abs(carry) & Abs(result))
end if
End Function

' Works out which integer (stored as a string) is larger
' this is used because the large numbers we are using are not capable of being stored
' in conventional VBScript Data Types
Function WhichIsBigger(num1, num2)
WhichIsBigger = 0
if Len(num1) > Len(num2) then
WhichIsBigger = 1
elseif Len(num2) > Len(num1) then
WhichIsBigger = 2
else
for i = 1 to Len(num1)
if (Int ( Mid (num1, i, 1) ) > Int ( Mid (num2, i, 1) ) ) then
WhichIsBigger = 1
i = Len(num1)+1
elseif (Int ( Mid (num2, i, 1) ) > Int ( Mid (num1, i, 1) ) ) then
WhichIsBigger = 2
i = Len(num1)+1
end if
next
end if

End Function

Sign in to follow this  


2 Comments


Recommended Comments

JScript doesn't even have integers... It only has a 64-bit floating point type called Number. [sad]

But, more to the point, JSCript can be used for WMI/WSH scripts too, you know - and it's far less annoying! And I'd probably say it has quite a larger feature set than VB (user-defined objects, prototypes (page works in IE), eval()).


function bignum(l, h){
this.low = l;
this.high = h;
}

function make_bignum(str){
/* do parsing stuff which I won't go into ;) */
var low = stuff();
var high = stuff();
//both low and high < pow([2],[32])
return new bignum(low, high);
}

bignum.prototype.add = function(b){
var low, high;
low = this.low + b.low;

var carry = [0];
if(low > [4294967295]){
carry = [1];
low -= [4294967296];
}

high = this.high + b.high + carry;
if(high > [4294967295])
throw "bignum too big mate! :o";

return new bignum(low, high);
}

//...etc



That's probably full of bugs, but it explains the principle. That would 64-bit if it was finished, it could be extended to 32*k bits, by making bignum contain an array of numbers. low and high are actually 64-bit integers, which are actually have a 48-bit integer part, IIRC. So it might be possible to get a few more bits out of them... [grin]

edit: for some reason it's put square brackets around my numbers, oh well.

Share this comment


Link to comment
Yeah, I really should look into javascript. I haven't given it as much time to learn really as VBScript. The main reason I started using VBScript is because it was company policy (as in, nobody else in the company has the skills to learn any other scripting language, and their current use of VBScript is patchy at best) and all of our standard build installation scripts are in VBScript. It makes it so much easier to just copy-paste what I need out of existing scripts to create a new better script for a specific task.

I do intend to learn javascript at some point in future, right after C# I believe it sits on my list ;)

Share this comment


Link to comment

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
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!