Skip to content

Commit 7311f08

Browse files
committed
fix!: Improve float()'s distribution (#36)
When a `min` and `max` is given, `float()` tends to stick around the `min`. This PR changes the implementation to ensure the values distribute more evenly across the (min, max) range of values. **BREAKING CHANGE** This is a breaking change. Inputs for `float()` will now map to a different output after upgrading fictional. It is still deterministic, the same input will always map to the same output. It is just that after the upgrade the corresponding output for each input will have changed. ### Before/After <img width="985" alt="Screenshot 2024-01-02 at 15 12 14" src="https://github.com/oftherivier/fictional/assets/1731223/df64be59-705c-499c-8218-516e20e13855"> ### Before on its own <img width="975" alt="Screenshot 2024-01-02 at 15 13 20" src="https://github.com/oftherivier/fictional/assets/1731223/42de34c1-229d-444a-8593-b461a918619d"> ### After on its own <img width="1003" alt="Screenshot 2024-01-02 at 15 14 04" src="https://github.com/oftherivier/fictional/assets/1731223/b32952ea-09f2-4988-abcd-1b366b05fc1c"> https://gist.github.com/justinvdm/9494ed9ba728de802778832b3c9d640d
1 parent a7cbd96 commit 7311f08

File tree

5 files changed

+53
-64
lines changed

5 files changed

+53
-64
lines changed

float.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
var hash = require('./hash')
22
var conj = require('./utils/conj')
3-
var fit = require('./utils/fit')
43
var defaults = require('./utils/defaults')
54

65
function float(input, opts) {
76
opts = opts || 0
8-
97
var min = defaults(opts.min, 0)
108
var max = defaults(opts.max, Number.MAX_SAFE_INTEGER)
11-
12-
// rehash to differentiate from `int`
13-
var whole = hash(hash(input))
14-
15-
var decimalPlaces = fit(hash(whole), 2, 5)
16-
var decimalDivisor = Math.pow(10, decimalPlaces)
17-
18-
var v = fit(whole, min, max) / decimalDivisor
19-
20-
return fit(v, min, max)
9+
return (hash(input) / Number.MAX_SAFE_INTEGER) * (max - min) + min
2110
}
2211

2312
float.options = function floatOptions(opts) {

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ value with both a whole and decimal segment.
334334

335335
```js
336336
float('id-23')
337-
// => 4139094247650.859
337+
// => 2211849950287729
338338
```
339339

340340
##### `options`
@@ -347,7 +347,7 @@ float('id-2', {
347347
min: 2,
348348
max: 99
349349
})
350-
// => 2.54
350+
// => 56.259760707962705
351351
```
352352

353353
#### <a name="dateString" href="#date-string">#</a> `dateString(id[, options])`

0 commit comments

Comments
 (0)