Re: Procedure for percent of a value
- From: Simon Bachmann <ssiimmoonnbbaacchhmmaannnn@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 29 Feb 2008 16:09:47 +0000
Marcos Martinez Sancho wrote:
Hello,
I need a small procedure for calculate the percent of a value
e.g.
set srate [expr int(($tps_ok / $number_of_total_tps)*100)]
puts "\nTEST RESULT: $number_of_total_tps tps of which $tps_ok PASSED and $tps_nok FAILED so $srate % succes rate"
doesnt look optimal and is not working except when srate = 100%. Otherwise, I get 0%.
When both numbers in a division operation are are integer, [expr] performs an integer division. That means that only the integer part of the result is calculated, no "comma part":
% expr 10/3
3
%
And since srate < 100% means that tps_ok is less than number_of_total_tps, the $tps_ok / $number_of_total_tps will always give 0 as result.
The solution is to make one of the values a floating point value, all other numbers will be "coerced" into floating point numbers too. Usually it's done like this:
set srate [expr int((1.0 * $tps_ok / $number_of_total_tps)*100)]
^^
Any input is appreciated.
Thanks
Marcos
--
The orthography of this message is a product of the phantasy of the autor.
Coincidence with existing rules is purely incidental and not intended.
.
- Follow-Ups:
- Re: Procedure for percent of a value
- From: Ron Fox
- Re: Procedure for percent of a value
- From: Cameron Laird
- Re: Procedure for percent of a value
- From: Marcos Martinez Sancho
- Re: Procedure for percent of a value
- References:
- Procedure for percent of a value
- From: Marcos Martinez Sancho
- Procedure for percent of a value
- Prev by Date: <treectrlWidget> item bbox doesn't work
- Next by Date: Re: Procedure for percent of a value
- Previous by thread: Re: Procedure for percent of a value
- Next by thread: Re: Procedure for percent of a value
- Index(es):
Relevant Pages
|