Rounding numbers in MT4 via NormalizeDouble - page 4

 
Dmitry Fedoseev:
This is because it is 0.6, so 0.5 is only obtained by cheating.

double v1 = 1.1234567;

If 'v1' is inserted in any number, then it shows that it is rounded correctly to 'i = 2'.

 
lilita bogachkova:

If 'v1' is inserted in any number, then you can see that up to 'i = 2' everything is rounded correctly.

Is it wrong after i = 2?
 
Ihor Herasko:

When only one digit is normalized, it is simple: 0, 1, 2, 3, 4 -> 0, and 5, 6, 7, 8, 9 -> 1.

When two digits are normalized, two-digit numbers are taken into account: 0 - 49 -> 0, and 50 - 99 -> 1. After all, if the number 1.49 has to be rounded to integers, do we really have to get 2, which is 51 hundredths vs. the available 49 hundredths distance to 1?

It is the same with three-digit, four-digit, etc.

But 1.45 is rounded up to 2, but only if it has previously been normalized to one degree higher precision.

void OnStart()
  {
   for(int i=7;i>=0;i--)
     {
      double v1 = 1.4545454;
      double v2 = NormalizeDouble(v1,i+1);

      v1=NormalizeDouble(v1,i);
      v2=NormalizeDouble(v2,i);
      Print("v1 = ",i," = ",DoubleToString(v1,7),", v2 = ",i," = ",DoubleToString(v2,7));
     }
     Print("---");
  }


v1 = 0 = 1.0000000, v2 = 0 = 2.0000000

v1 = 1 = 1.5000000, v2 = 1 = 1.5000000

v1 = 2 = 1.4500000, v2 = 2 = 1.4600000

v1 = 3 = 1.4550000, v2 = 3 = 1.4550000

it turns out that if in the calculations of the number was normalized to a higher accuracy and in subsequent calculations to normalize this number to a lower accuracy will result in a different from the simply normalized this number to a lower accuracy.


 
lilita bogachkova:

it turns out that if in the calculation the number was normalised to a higher accuracy and in subsequent calculations the number is normalised to a lower accuracy, the result is different from just normalising the number to a lower accuracy.


Above such actions are called "cheating")) Mathematics, although an exact science, must still be applied with understanding. 2 + 3 = 5, but if 2 buckets plus 3 chickens, then 5 will not work ))
 

Excuse me, but how do you get the values you want?

If I use the recommended functions, then:

- In one case I get correctly rounded value
0.06-0.02 =NormalizeDouble(0.03999999999999999,2) = 0.04
The division is approximated and normalization is required.

- In the second case, I also get rounding, but it is not needed in this case
0.06-0.024 = NormalizeDouble(0.036,2) = 0.04
Normalization (rounding) is not needed here.

But how do I normalize instead of rounding?

 
Roman Starinskij:

Excuse me, but how do you get the values you want?

For that, it's just worth formulating what the "right values" are ))

If I use the recommended functions, then:

- In one case I get a properly rounded value
0.06-0.02 = NormalizeDouble(0.03999999999999999,2) = 0.04
Dividing is approximated and you can't do without normalization.

- In the second case, I also get rounding, but it is not needed in this case
0.06-0.024 = NormalizeDouble(0.036,2) = 0.04
Normalization (rounding) is not required here.

But how do I normalise properly, rather than rounding the values?

The example above shows that you're using some intuitive algorithm that determines the "rightness" of normalization. In the first case, it is an infinite fraction, while in the second case it is a finite one. Adjust your algorithm for converting the real number to the desired form.

Although the easiest way - decide in advance on the required accuracy of calculations.

 
Ihor Herasko:

To do this, it is simply worth formulating what the 'right values' are ))

From the above example, you can see that you are using some intuitive algorithm, by which you determine the "necessity" of normalization. In the first case, it is an infinite fraction, while in the second case it is a finite one. Adjust your algorithm for converting the real number to the desired form.

Although the easiest way is to decide in advance what accuracy you need.


I just need to get the same numbers, which I used to get in the calculator. :-) These are the values you need.
So that at "0.06-0.02" you get "0.04" and at "0.06-0.024" you get "0.036".
 
Roman Starinskij:

I just need to get the same numbers that I'm used to getting in a calculator. :-) These are the values you need.
If you want "0.06-0.02" you will get "0.04" and if you want "0.06-0.024" you will get "0.036".
If you want to get 0.036 (3 decimal places), you need to normalize to 3 decimal places, not 2.
 
Dmitry Fedoseev:
If you want 0.036 (3 decimal places), normalize to 3, not 2.

I was wrong, up to 3 digits should be normalised. Is it possible to do this?

 
Roman Starinskij:

I was wrong, up to 3 digits should be normalised. Will it work that way?

Here's your code:

0.06-0.024 = NormalizeDouble(0.036,2) = 0.04

Instead of 2, put 3 and it's 0.036.

Reason: