Page 1 of 1 1
Topic Options
#213905 - 2020-08-12 03:30 PM Simple Division
JNK Offline
Fresh Scripter

Registered: 2006-04-11
Posts: 32
ok, what am I doing wrong? I need to calculate (divide) 2 numbers and then multiply by 100 to get the average. Kix can add, subtract and multiple but can't divide?

$foo = 5
$bar = 10

$foo + $bar
- returns 15

$foo - $bar
- returns -5

$foo * $bar
- returns 50

... but ... $foo / $bar
- returns ... zero?

Top
#213906 - 2020-08-12 04:04 PM Re: Simple Division [Re: JNK]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4520
Loc: USA
It's partly the way you defined your numbers and it's the way Kix generally handles numbers. It can be worked around though.

FTR, if you had tried $bar / $foo it would have worked.

You can define a number as Double to avoid the problem. So instead of 5 you can define as 5.0. Or you can use the function CDBL to do it, ie CDBL($foo).


Top
#213907 - 2020-08-12 04:59 PM Re: Simple Division [Re: Allen]
JNK Offline
Fresh Scripter

Registered: 2006-04-11
Posts: 32
I understand what you are saying. My data is being retrieved via a WMI query and the calculation is actually part of a script to determine the percentage of free disk space. Casting the value as Double (CDBl) was probably the only function I didn't try.

... one moment, please ...

Top
#213908 - 2020-08-12 05:36 PM Re: Simple Division [Re: Allen]
JNK Offline
Fresh Scripter

Registered: 2006-04-11
Posts: 32
Casting the value(s) as CDBL worked!

... I think that is the only options I didn't try before!

I knew it had to be something stupid simple.

As always, thank you @allen

Top
#213909 - 2020-08-12 06:06 PM (NA) Re: Simple Division [Re: JNK]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4350
Loc: New Jersey
$data = WmiQuery(get disk things...)

$FreePct = 1.0 * $data.free / $data.size

Multiplying by 1.0 FIRST tells Kix that the entire operation should use double-precision math without calling a function or changing the values.

Adding 0.0 to an add/subtract has the same net effect.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#213910 - 2020-08-12 06:10 PM (NA) Re: Simple Division [Re: Glenn Barnas]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4350
Loc: New Jersey
BTW - What platform are you using for monitoring, or is this just for information gathering and reporting?
_________________________
Actually I am a Rocket Scientist! \:D

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 301 anonymous users online.
Newest Members
Praveer, Ollero, rayyo, Foxlicht, DonChild
17742 Registered Users

Generated in 0.032 seconds in which 0.011 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org