Skip to content

round option

Jongmoon Yoon edited this page Sep 25, 2019 · 1 revision

round option

Necessity

Returning calculated coordinates without rounding is the default behavior of Axes.

Since Axes 2.6, the round option has been added. The round option allows you to change the value in the units you want.

Specifying the round option has the following expected effects:

1. You can avoid floating point issues as much as possible.

Just as you can't represent 10/3 = 3.33333 .... in a decimal number as a fully-delimited decimal number, you may encounter a situation where binary values ​​are not fully divided even on computers that use binary. This will result in rounding of the binary number.

So a situation arises where 0.1 + 0.2! == 0.3.

Reference

[IEEE 754] 0.1 + 0.2 = 0.30000000000000004? 0.1 + 0.2 ≠ 0.3?

https://floating-point-gui.de/basic/

This problem can be avoided by rounding up to a certain number of digits because the problem usually occurs at a large digit. For example, the code view360 contains the following code:

const fovRange = this.options.fovRange.map (v => + v.toFixed (5));

However, Axes provides a round option to avoid floating point issues.

2. You do not need to round them individually for each position that needs rounded values.

To avoid floating point problems like this, you need to round the values ​​received from Axes throughout your service code. This not only increases the cost of managing the code, but can also reduce performance due to the application of an unoptimized solution (eg toFixed).

3. You can receive the value in a certain number unit.

In addition, you can receive the values ​​in the unit you want.

Axes by default passes non-rounded values ​​like 9.92939177071014. However, depending on the round value, you can get the value in the unit you want.

If the range of Axes values ​​is [0, 10],

The default (round: null) returns something like [0.123456 ...., 1.2334344, ....., 9.999999, 10]. However, if you specify a round value, it returns:

  1. If the round value is 0.1, [0, 0.1, 0.2, ...., 9.8, 9.9, 10]
  2. If round value is 1, [0, 1, 2, 3, 4, .... 9, 10]
  3. If the round value is 5, [0, 5, 10]
  4. If the round value is 10, [0, 10]

How to use

The interface of Axes is as follows:

Axes (axis: Object, options: AxesOption, startPos: <string, number>)

Note: Axes API

To use the round option, specify a round value for options. Note: Note that you specify the round option for the second parameter, not the axis as the first parameter.

const axes = new eg.Axes ({
  x: {
    range: [0, 100]
},
  y: {
    range: [0, 100]
  }
}, {
 round: 1 / * round to 1 * /
});

example

See the difference as you change the round options in the following Codepen example.

https://codepen.io/egjs/pen/xxKBVEL