cancel
Showing results for
Show  only  | Search instead for
Did you mean:

## Calculate a percentage?

Visitor

Given two fields:

genCount = 2

vecEmbeddingCount = 2

I want to calculate the percentage of genCount of the total (ie. The result I'm looking for is 50%).

``````fetch logs
...
| fieldsAdd totalCount = genCount + vecEmbeddingCount
| fieldsAdd cacheHitRate = genCount / totalCount*100``````

What am I missing?

3 REPLIES 3
Dynatrace Mentor

Order of operations matters here, especially these are integer operations:
2/4*100 = 0*100 = 0
but

100*2/4 = 200/4 = 50.

Here is DQL example:

``````data record(genCount = 2,vecEmbeddingCount = 2)
| fieldsAdd totalCount = genCount + vecEmbeddingCount
| fieldsAdd cacheHitRate = genCount / totalCount*100
| fieldsAdd cacheHitRateCorrect = 100*genCount / totalCount``````

Kris

Visitor

Where is it specified that these are integers (or integer operations)?
In fact, integers aren't even mentioned in the DQL data types page - only double and long are listed.

This seems to work:

``````fetch logs
| summarize genCount = toDouble(countIf(matchesPhrase(content, "gpt-3.5"))),
| fieldsAdd totalCount = genCount + vecEmbeddingCount
| fieldsAdd cacheHitRate = genCount / totalCount * 100
| fieldsKeep cacheHitRate``````
Dynatrace Mentor

I did not mean "integer data type", but meant "integer mathematical concept" (https://en.wikipedia.org/wiki/Integer)

When you convert "long" numbers to "double" numbers (floating point) it will work too, but will be give you result in form of "double". What type you want to get in the end of course depends on your requirements.

When you get result in form of floating point number, you can always get needed precision using round (https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/functions/mathematical-funct...) or needed data type i.e. using toLong (https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/functions/conversion-and-cas...)

Re: "order of operations" which often matter when dealing with integers . DQL uses most common approach uses by programming languages ("left to right"), probably that's why it was never mentioned in documentation. Example in second paragraph here covers similar case like ours: https://en.wikipedia.org/wiki/Order_of_operations#Programming_languages

Kris

Featured Posts