Skip to content

Commit d02754f

Browse files
Copilotstreamich
andcommitted
Fix floating-point precision issues in Random.num for integer formats
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
1 parent 4b69007 commit d02754f

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

packages/json-type/src/random/Random.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ export class Random {
114114
let max = Number.MAX_SAFE_INTEGER;
115115
const schema = type.getSchema();
116116
const {lt, lte, gt, gte} = schema;
117+
const isIntegerFormat = schema.format && ['i8', 'i16', 'i32', 'i64', 'i', 'u8', 'u16', 'u32', 'u64', 'u'].includes(schema.format);
117118
if (gt !== undefined) min = gt;
118119
if (gte !== undefined)
119120
if (gte === lte) return gte;
120-
else min = gte + 0.000000000000001;
121+
else min = isIntegerFormat ? gte : gte + 0.000000000000001;
121122
if (lt !== undefined) max = lt;
122-
if (lte !== undefined) max = lte - 0.000000000000001;
123+
if (lte !== undefined) max = isIntegerFormat ? lte : lte - 0.000000000000001;
123124
if (min >= max) return max;
124125
if (schema.format) {
125126
switch (schema.format) {

0 commit comments

Comments
 (0)