Math question....

Want to talk about something that isn't covered by another category?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
bogs
Posts: 5150
Joined: Sat Feb 25, 2017 10:45 pm

Math question....

Post by bogs » Tue Jun 25, 2019 4:12 pm

So, I was doodling around with a math problem, and I am not sure I understand it correctly, so even though I got a result, I was hoping someone could illustrate where I might be going wrong, if I am going wrong.

The numbers in the equation are for calculating the cpu load on linux. I read (and think I understand) the explanation given on the pages I've read in this area.

The numbers for the equation come in from one line in a file. The equation is laid out like this:

Code: Select all

 
X % = ( 842486413 * 100 ) / ( 79242 + 0 + 74306 + 842486413 + 756859 + 6140 + 67701 + 0 ) = ( 842486413100 ) / ( 843470661 ) = 99.8833 %
The first part, 842486413, comes from item 4 of the second part. Rather than put the items in the parts of the equation, I was trying to think of another way to arrive at the same answer. This is the code I used to put it together:

Code: Select all

on checkProc
//other code unrelated...
      put line 1 of url("File:~/theFile") into tmpProc # the file changes constantly...
      set the itemDelimiter to space
      put (item 2 to -1 of line 1 of tmpProc) into tmpAvg
      delete character 1 of tmpAvg # space I don't want converted to comma...
      replace space with comma in tmpAvg
      put sum(tmpAvg) into tmpAvgSum # total of the 2nd part of the equation...
      set the itemDelimiter to comma
      put item 4 of tmpAvg *100 into tmpAvgIdle # first part of the equation....
      put tmpAvgIdle/ tmpAvgSum into tmpAvgPrcnt # the part I'm not sure is right...
end checkProc
So am I completely missing it, or does that look like it should arrive at the correct answer? And yes, I am sure there are better ways to do it hee hee, but right now I am just trying to arrive at a level of understanding.
Image

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Math question....

Post by [-hh] » Tue Jun 25, 2019 6:09 pm

The number 842486413100 in your first code line is wrong:
842486413*100 is not 842486413100 ...

Your handler is OK. Here is yet another approach.

Code: Select all

function checkProc tP
  repeat for each word w in tP
    add w to S
  end repeat
  return round((100*word 4 of tP)/S,4) & " %"
end checkProc
For example

Code: Select all

on mouseUp
  put checkProc(" 79242 0 74306 842486413 756859 6140 67701 0 ")
end mouseUp
yields 99.8833 %.
shiftLock happens

bogs
Posts: 5150
Joined: Sat Feb 25, 2017 10:45 pm

Re: Math question....

Post by bogs » Tue Jun 25, 2019 8:05 pm

[-hh] wrote:
Tue Jun 25, 2019 6:09 pm
The number 842486413100 in your first code line is wrong:
842486413*100 is not 842486413100 ...
That is kind of my fault for not proof reading the example on this page, from which I copied and pasted without proof reading...
Based on the example above:
average idle percentage X % = ( 842486413 * 100 ) / ( 79242 + 0 + 74306 + 842486413 + 756859 + 6140 + 67701 + 0 ) = ( 842486413100 ) / ( 843470661 ) = 99.8833 %
Looks like a typo that got past the author and myself heh.

Thanks for the function, I knew there was a better way to do it, but like I say, I was mostly just trying to make sure I was following the math correctly. Math isn't my strongest suit, unfortunately :?

Since I'll be processing 4 lines from that file, which look like this:

Code: Select all

cpu  49196 196 84826 108450 12774 0 1823 0 0 0
cpu0 16342 63 29670 35541 4249 0 646 0 0 0
cpu1 16661 40 26728 36838 4167 0 1070 0 0 0
cpu2 16192 93 28427 36069 4356 0 106 0 0 0
it is almost certain I was going to have to reduce it to a function anyway, yours is far better than the way I was thinking of going :D
Image

Post Reply

Return to “Off-Topic”