Functional Weave
Code in TypeScript

math.round-float@1.0.0

vectors.json

2,264 bytes · view raw

[
  {"name": "a half rounds up to 1, not to the even 0", "args": [0.5, 0], "expect": 1},
  {"name": "2.5 rounds away from zero to 3 (Python's round() gives 2)", "args": [2.5, 0], "expect": 3},
  {"name": "-2.5 rounds away from zero to -3 (Math.round gives -2)", "args": [-2.5, 0], "expect": -3},
  {"name": "1.5 rounds to 2", "args": [1.5, 0], "expect": 2},
  {"name": "0.125 is exact in binary, so it is a true tie and goes up", "args": [0.125, 2], "expect": 0.13},
  {"name": "-0.125 is a true tie and goes down", "args": [-0.125, 2], "expect": -0.13},
  {"name": "1.005 is stored as 1.00499999..., so it rounds down", "args": [1.005, 2], "expect": 1},
  {"name": "2.675 is stored as 2.67499999... (2.675 * 100 is exactly 267.5 in floating point, which fools the shortcut)", "args": [2.675, 2], "expect": 2.67},
  {"name": "8.345 is stored as 8.34500000000000064, so it rounds up", "args": [8.345, 2], "expect": 8.35},
  {"name": "1.45 is stored just below the tie", "args": [1.45, 1], "expect": 1.4},
  {"name": "0.0005 is stored just above the tie", "args": [0.0005, 3], "expect": 0.001},
  {"name": "the largest double below a half rounds to 0 (floor(x + 0.5) gives 1)", "args": [0.49999999999999994, 0], "expect": 0},
  {"name": "binary noise from 0.1 + 0.2 is removed at 12 places", "args": [0.30000000000000004, 12], "expect": 0.3},
  {"name": "a small negative rounds to zero, not minus zero", "args": [-0.001, 2], "expect": 0},
  {"name": "an integer is unchanged", "args": [-7, 3], "expect": -7},
  {"name": "zero places", "args": [1234.5678, 0], "expect": 1235},
  {"name": "one place", "args": [123.456, 1], "expect": 123.5},
  {"name": "six places", "args": [1.0000005, 6], "expect": 1.000001},
  {"name": "beyond 2^52 in scaled units the double has no digits left to round, and is returned as it is", "args": [123456.78901234567, 12], "expect": 123456.78901234567},
  {"name": "more than 12 places is an error", "args": [1.5, 13], "expectError": "decimals must be a whole number from 0 to 12"},
  {"name": "negative places is an error", "args": [1.5, -1], "expectError": "decimals must be a whole number from 0 to 12"},
  {"name": "fractional places is an error", "args": [1.5, 1.5], "expectError": "decimals must be a whole number from 0 to 12"}
]