Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property 'Values' in conflict #96

Open
iamterryclark opened this issue Oct 2, 2020 · 3 comments
Open

Property 'Values' in conflict #96

iamterryclark opened this issue Oct 2, 2020 · 3 comments
Labels
bug Something isn't working

Comments

@iamterryclark
Copy link

Hey,

I am using react-range with a min / max of 0 and 1 and a step of 0.001. the values I am sending is within an array.

[0.66, 0.947]

I seem to get the warning appear on component load. Am I missing something as this seems in range to me..

Thanks in advance

@tajo tajo added the bug Something isn't working label Oct 2, 2020
@tajo
Copy link
Owner

tajo commented Oct 2, 2020

Something's broken about multiple thumbs and decimals.

@raptor3790
Copy link

Any news on this issue?

@swashata
Copy link

swashata commented Aug 10, 2021

I can say that this is due to floating point arithmetic in JavaScript.

Let's say, my config is like this

{
	min: 97,
	max: 108,
	step: 0.1,
	values: [98.6],
}

During the calculation at

values.forEach((value) => {
if (!isStepDivisible(min, value, step)) {
console.warn(
'The `values` property is in conflict with the current `step`, `min`, and `max` properties. Please provide values that are accessible using the min, max, and step values.'
);
}
});

It becomes

react-range/src/utils.ts

Lines 17 to 20 in 57345c3

export function isStepDivisible(min: number, max: number, step: number): boolean {
const res = (max - min) / step;
return parseInt(res.toString(), 10) === res;
}

const res = (max - min) / step;
// (98.6 - 97) / 0.1
// 15.999999999999943
return parseInt(res.toString(), 10) === res;
// 15 === 15.999999999999943
// false

One possible solution is to take a precision prop from userland and multiply the numbers to convert floats to integer.

Like

const multiplier = 10 ** precision;
const normalizedMax = Math.round(max * multiplier);
const normalizedMin = Math.round(min * multiplier);
const normalizedStep = Math.round(step * multiplier);
const res = (max - min) / step;
// (986 - 970) / 1
// 16
return parseInt(res.toString(), 10) === res;
// 16 === 16
// true

I have forked this library and made such changes. I cannot think of any other method where I can reliably extract number of decimal points from given values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants