JNK
(Fresh Scripter)
2020-08-12 03:30 PM
Simple Division

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?


AllenAdministrator
(KiX Supporter)
2020-08-12 04:04 PM
Re: Simple Division

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).



JNK
(Fresh Scripter)
2020-08-12 04:59 PM
Re: Simple Division

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 ...


JNK
(Fresh Scripter)
2020-08-12 05:36 PM
Re: Simple Division

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


Glenn BarnasAdministrator
(KiX Supporter)
2020-08-12 06:06 PM
(NA) Re: Simple Division

$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


Glenn BarnasAdministrator
(KiX Supporter)
2020-08-12 06:10 PM
(NA) Re: Simple Division

BTW - What platform are you using for monitoring, or is this just for information gathering and reporting?