react-component / react-component/slider

there is a precision issue, when you set 'props.min' or 'props.max' as a float number.

Open
#667 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.1k
Forks
768
Avg merge
11d 22h
Merged PRs (30d)
5

Description

<Slider
    min={0.1}
    max={2}
    step={0.1}
    value={this.state.zoom}
    onChange={zoom => {
        this.setState({ zoom });
    }}
/>

screenshot:
image

Problem: max will never be 2, it forever only can be 1.9。

Let's View The Reason:
in this function, from this code: Math.floor((max - min) / step), number miss its precision.

// source code url: /src/utils.ts

export function getClosestPoint(val: number, { marks, step, min, max }) {
    const points = Object.keys(marks).map(parseFloat);
    if (step !== null) {
        const maxSteps = Math.floor((max - min) / step); // from this line code, number miss its precision.
        // calc steps:
        // (max - min) / step => (2 - 0.1) / 0.1 = 18.999999999999996
        // Math.floor((max - min) / step) => Math.floor(18.999999999999996) = 18
        // maxSteps = 18

        const steps = Math.min((val - min) / step, maxSteps);
        // steps = 18

        const closestStep = Math.round(steps) * step + min;
        // Math.round(steps) * step + min => 18 * 0.1 + 0.1 = 1.9000000000000001
        // closestStep = 1.9000000000000001
        // so far, you will get closestStep value be: 1.9000000000000001, not 2

        points.push(closestStep);
    }
    const diffs = points.map(point => Math.abs(val - point));
    return points[diffs.indexOf(Math.min(...diffs))];
}

How to resolve this problem?

  1. adjust source code
  2. use integer number to avoid this precision problem.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/utils.ts at getClosestPoint and reproduce the reported calculation with min=0.1, max=2, and step=0.1. Verify that selecting the upper bound can return 2 rather than 1.9, while preserving closest-point behavior for fractional values.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.