# NormalizeDouble(Bid, Digits) - page 2

20339

Petr Nosek: What is wrong with this code:
Assumes that ND rounds or truncates. Write self-documenting code and specify exactly which you mean.
MT4:NormalizeDouble - General - MQL5 programming forum
How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum
2400

whroeder1:
Assumes that ND rounds or truncates. Write self-documenting code and specify exactly which you mean.
MT4:NormalizeDouble - General - MQL5 programming forum
How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum

This is wrong.

`double MathNearest(  double v, double to){ return to * MathRound(v / to); }`

it should be

`double MathNearest(  double v, double to){ return to * NormalizeDouble(v / to, 0); }`
20339

nicholishen: This is wrong. it should be
You assume ND(x,0) rounds to the nearest integer. MathRound always does to the nearest int by definition. ND use is wrong.
3132

whroeder1:
Assumes that ND rounds or truncates. Write self-documenting code and specify exactly which you mean.

Is there anyone else who did not understand this simple example?
For whroeder1:
In this example I have a variable "profitUSD" (self-documenting). Its value is e.g. profit in USD and I want to find out profit in EUR but just on two decimal places. I use ND to set the variable "profitEUR" (self-documenting).
And now I want you to explain what is wrong with using ND.

2400

whroeder1:
You assume ND(x,0) rounds to the nearest integer. MathRound always does to the nearest int by definition. ND use is wrong.

What is this nonsense? Neither of those returns an int!

Let's prove this quantitatively with a simple script.

```void OnStart()
{
double test_number = 1.700605;
double step = 0.00001;
string result_str = "The expected result is 1.70061, and the actual result is";
printf(
"method: \"MathRound()\", %s %s",
result_str,
DoubleToString(step * round(test_number / step),5)
);

printf(
"method: \"NormalizeDouble()\", %s %s",
result_str,
DoubleToString(step * NormalizeDouble(test_number / step, 0),5)
);
}```

2400

I just wanted to comment that there's no shame in admitting when one is wrong. That's what science is all about, adapting to new evidence as it is brought to light. I, for one, will gladly admit to being proved wrong with new evidence - because I'd rather be correct and humble than arrogant and wrong. That being said, if there's anyone in particular whro's in denial, I say, embrace the new evidence! Perhaps a simple change in language is the difference between being correct and being totally wrong... perhaps one should change one's phrasing to something like, "its use is always usually wrong".
3132

nicholishen:
I just wanted to comment that there's no shame in admitting when one is wrong. That's what science is all about, adapting to new evidence as it is brought to light. I, for one, will gladly admit to being proved wrong with new evidence - because I'd rather be correct and humble than arrogant and wrong. That being said, if there's anyone in particular whro's in denial, I say, embrace the new evidence! Perhaps a simple change in language is the difference between being correct and being totally wrong... perhaps one should change one's phrasing to something like, "its use is always usually wrong".
I totally agree with this.
20339

`printf("%7.2f %7.2f %7.5f",test_number / step, round(test_number / step),step * round(test_number / step));// 170060.50 170060.00 1.70060`
Congratulations, you found a bug in MathRound.
2400

whroeder1:
Congratulations, you found a bug in MathRound.

Ok... next step... since NormalizeDouble works correctly - what is your final conclusion?

38992

Petr Nosek:
I totally agree with this.

Me too. That's a five years old discussion with WHRoeder, he never understood it, I never understood why he didn't.