'Does prometheus round off values for the divsion operator?
I have a query like sum(query1)/sum(query2). When I manually do the division, it gives 1.98 but in result it says 2. Is that doing it automatically ?
Solution 1:[1]
I suspect this rounding happens on your visualization side, like Grafana. I've seen crazy decimals in Prometheus and used round() (see https://prometheus.io/docs/prometheus/latest/querying/functions/#round) to force rounding in some occasions.
Or did you run the query in Prometheus Graph and saw this behaviour?
Solution 2:[2]
Prometheus stores time series values as 64-bit floating-point numbers and preforms all the PromQL calculations with floating-point arithmetic, which may result in some precision loss. See, for example, the result returned by Prometheus for 0.1+0.2. See also these explanations. But this precision loss is invisible in most practical calculations.
So the answer to the original questions: Prometheus doesn't round off values for the division operator and for any other PromQL operations. But some precision loss may happen in edge cases due to floating-point math.
Prometheus provides round() function, which can be used for rounding the results to the given decimal digit. For example, round(vector(1/3), 0.1) would return 0.3. The vector() function is needed for Prometheus only due to PromQL implementation details. It isn't needed for other Prometheus-compatible query engines such as MetricsQL at VictoriaMetrics.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sskrlj |
| Solution 2 | valyala |
