Fix potential overflow error on toPC (#1458)

One might want to rework that toPC code to avoid this oddities.
Consider this a quick fix.
This commit is contained in:
Kim De Mey 2023-02-02 09:30:14 +01:00 committed by GitHub
parent 7cb146c037
commit ba92c53624
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 0 deletions

View File

@ -62,8 +62,13 @@ proc toPC*(
let
sign = if num < 0: "-" else: ""
preTruncated = (num.abs * multiplier) + roundUp
if int.high.float <= preTruncated:
return "NaN"
# `intToStr` will do `abs` which throws overflow exception when value of low()
if preTruncated.int <= int.low():
return "NaN"
result = sign & preTruncated.int.intToStr(minDigits) & "%"
when 0 < digitsAfterDot:
result.insert(".", result.len - minDigits)