Simply use `math.Mod(float64, float64)` after all

This circumvents all the problems with int overflow, plus it is what was originally intended.
This commit is contained in:
beorn7 2016-11-08 21:03:31 +01:00
parent 5cf5bb427a
commit 4e3abc6cbf
2 changed files with 6 additions and 23 deletions

View File

@ -1037,10 +1037,7 @@ func scalarBinop(op itemType, lhs, rhs model.SampleValue) model.SampleValue {
case itemPOW:
return model.SampleValue(math.Pow(float64(lhs), float64(rhs)))
case itemMOD:
if int64(rhs) != 0 && convertibleToInt64(lhs) && convertibleToInt64(rhs) {
return model.SampleValue(int64(lhs) % int64(rhs))
}
return model.SampleValue(math.NaN())
return model.SampleValue(math.Mod(float64(lhs), float64(rhs)))
case itemEQL:
return btos(lhs == rhs)
case itemNEQ:
@ -1071,10 +1068,7 @@ func vectorElemBinop(op itemType, lhs, rhs model.SampleValue) (model.SampleValue
case itemPOW:
return model.SampleValue(math.Pow(float64(lhs), float64(rhs))), true
case itemMOD:
if int64(rhs) != 0 && convertibleToInt64(lhs) && convertibleToInt64(rhs) {
return model.SampleValue(int64(lhs) % int64(rhs)), true
}
return model.SampleValue(math.NaN()), true
return model.SampleValue(math.Mod(float64(lhs), float64(rhs))), true
case itemEQL:
return lhs, lhs == rhs
case itemNEQ:

View File

@ -35,8 +35,8 @@ eval instant at 50m SUM(http_requests) BY (job) % 3
{job="app-server"} 2
eval instant at 50m SUM(http_requests) BY (job) % 0.3
{job="api-server"} NaN
{job="app-server"} NaN
{job="api-server"} 0.1
{job="app-server"} 0.2
eval instant at 50m SUM(http_requests) BY (job) ^ 2
{job="api-server"} 1000000
@ -54,10 +54,9 @@ eval instant at 50m SUM(http_requests) BY (job) % 2 ^ 3 ^ 2
{job="api-server"} 488
{job="app-server"} 40
# int64 overflow on RHS of %.
eval instant at 50m SUM(http_requests) BY (job) % 2 ^ 3 ^ 2 ^ 2
{job="api-server"} NaN
{job="app-server"} NaN
{job="api-server"} 1000
{job="app-server"} 2600
eval instant at 50m COUNT(http_requests) BY (job) ^ COUNT(http_requests) BY (job)
{job="api-server"} 256
@ -352,13 +351,3 @@ eval instant at 5m metricA + ignoring() metricB
eval instant at 5m metricA + metricB
{baz="meh"} 7
clear
load 5m
finite{foo="bar"} 42
almost_zero{foo="bar"} 0.123
# MOD by "almost zero" with vector.
eval instant at 5m finite % almost_zero
{foo="bar"} NaN