Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Plugins ➜ Big numbers

Big numbers

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Klonical   (3 posts)  Bio
Date Thu 16 Oct 2003 04:39 PM (UTC)
Message
I tried to use the health bar with large numbers
and it crash with an overflow error.

Then I tried to replace
pc = CInt ((CInt (iCurrent) / CInt (iMax)) * 10)

to
pc = CLng ((CLng (iCurrent) / CLng (iMax)) * 10)

And have a type mismatch

How should I do this?

Ty.
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #1 on Fri 17 Oct 2003 02:54 AM (UTC)
Message
Erm, are you trying to do something like converting string "234,7632" to long 234.7632?
Top

Posted by Shadowfyr   USA  (1,792 posts)  Bio
Date Reply #2 on Fri 17 Oct 2003 05:03 AM (UTC)
Message
Long is a 32-bit integer Ked, not a decimal number.

I have had some issues with CLng and CInt. It seems that sometimes VBScript gets confused about what is actually going on. A better option is to do:

iCurrent = CLng(iCurrent)
iMax = CLng(iMax)
pc = CLng((iCurrent / iMax) * 10)

This should either tell you which of your values is causing a problem or fix it.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Fri 17 Oct 2003 05:21 AM (UTC)
Message
Man, if VBscript actually lets you do that, it's more screwed up that I think... because iCurrent is defined as an integer (right?), and setting assigning a long to iCurrent (type int) shouldn't change iCurrent to a long.

Your problem is probably coming from the type of pc, not the type of the other stuff. If you're assigning a long to the int, it may well be complaining at you.

You don't need to convert that value to a long, you can leave it as an int. If you look at what the code is actually doing - 10 * current/max - you'll see that it's a ratio, and technically speaking won't ever go above 10. The only way for it to equal 10 is if current == max, which makes current/max == 1, and 10*1 is obviously 10. And unless your variable names are very poorly chosen, current will never be greater than max.

Therefore, I would recommend you use the following:
pc = CInt ((CLng (iCurrent) / CLng (iMax)) * 10)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Shadowfyr   USA  (1,792 posts)  Bio
Date Reply #4 on Fri 17 Oct 2003 06:09 AM (UTC)
Message
Well Ksilyan, you can't actually define a variable as any specific 'type'. When data is put into a variable in VBScript the engine 'guesses' at which 'type' would be the best one. So unless pc already contains some value, like an object reference or an integer, but you you try to stick a decimal into it, you won't get any complaints. VBScript uses type Variant for 'all' variables. It only casts it to a specific type once you put something in.

The irony of this is that changing cast can be easier (though it can sometimes decide to use its own type, and ignore what you want). A good example is A = CLgn(2), which may turn A into a Integer, instead of Long, forcing you to recast it to Long when you display the value. This will even happen sometimes if the value will 'fit' in an Integer, but only as a negative value.

Once in a while it will get completely confused though. In this case the code is likely doing something like:

iCurrent = wildcards(1) 'Certain to be a string.
iMax = Some prior value, probably a string.
pc = CLng(CLng("-String-") / CLng("-Strng-") * 10)

VBScript has on several occations gone nuts and had a heart attack trying to figure out what it is supposed to do for me with things like this, which is why it is better if the value from a wildcard to is forced into the type you need, before/while you try puting it in something else. You never know what you may end up with otherwise, especially since pc has no Type, except Variant and the value Empty before you put something in it. In this case it could be something as silly as the script seeing " 45 " and getting confused about 'what' the contents really are when you ask it to convert. You run into the same problem when you use type Variant in VB 6.0, but it is worse in VBSCript because there is no way to specifically tell it what 'type' you want. You are only allowed 'Dim <Name>', where full VB allows 'DIM <Name> As <Type>'. If you use the VB version it either does produces an error or ignores it.
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #5 on Fri 17 Oct 2003 06:29 AM (UTC)
Message
Yeah sorry, I realized what I wrote right after hitting the Send button. But I always tend to reply to my own thoughts at the moment rather than the actual question :P
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


20,365 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.