why return many decimal ?? also after normalize double?

 

Hi, I create  this  simple  script  i notice  in some  cross  return many decimal and  if  i normalize double  not  normalize nothing  how  is possible?? i do some  error ??

return 2023.09.09 11:11:35.465    da_cancellare AUDCAD,H1: FirstPriceVCFirstPriceVCFirstPriceVCFirstPriceVCFirstPriceVC---> 86.52200000000001

my code

//+------------------------------------------------------------------+
//|                                                da_cancellare.mq4 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
       double valore1 = 1.1234500000;
    double valore2 = iOpen("NZDJPY", 0, 24);
          Print ("FirstPriceVCFirstPriceVCFirstPriceVCFirstPriceVCFirstPriceVC---> "+NormalizeDouble(valore2,7));

    double risultato1 = ArrotondaDinamicamente(valore1);
    double risultato2 = ArrotondaDinamicamente(valore2);

    Print("Risultato 1: ", DoubleToStr(risultato1,5)); // Stampa 1.12345
    Print("Risultato 2: ", DoubleToStr(risultato2,5)); // Stampa 45612.3
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
  
  
//+------------------------------------------------------------------+
double ArrotondaDinamicamente(double valore) {
    string valoreStringa = DoubleToString(valore, 15); // Converte il valore in una stringa con 15 cifre decimali massime
    int lunghezzaStringa = StringLen(valoreStringa); // Calcola la lunghezza della stringa
    int cifreDecimali = 0;

    // Conta il numero di cifre decimali alla fine della stringa
    for (int i = lunghezzaStringa - 1; i >= 0; i--) {
        if (StringGetCharacter(valoreStringa, i) != '0' && StringGetCharacter(valoreStringa, i) != '.') {
            cifreDecimali = lunghezzaStringa - i - 1;
            break;
        }
    }

    return NormalizeDouble(valore, cifreDecimali);
}
 
faustf:   i notice  in some  cross  return many decimal

You used NormalizeDouble, It's use is usually wrong, as it is in your case.

  1. Floating point has an infinite number of decimals, it's you were not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
              Double-precision floating-point format - Wikipedia, the free encyclopedia

    See also The == operand. - MQL4 programming forum (2013)

  2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

  3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
              On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum #10 (2011)

    And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

  4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
              Trailing Bar Entry EA - MQL4 programming forum (2013)
              Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

  5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
              (MT4 2013)) (MT5 2022))

  6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
              MT4:NormalizeDouble - MQL5 programming forum (2017)
              How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

  7. Prices you get from the terminal are already correct (normalized).

  8. PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum (2014)

 

Sorry but i have also again a problem when i try to call i open (you tell is just normalized a decimal, therefore i not normalize double ) but return some data wrong not normalized

DEMA DE MA DEEEE  -----> 23.07
DEMA DE MA DEEEE  -----> 88.98999999999999   NZDJPY
DEMA DE MA DEEEE  -----> 7226.6
DEMA DE MA DEEEE  -----> 15777.2
DEMA DE MA DEEEE  -----> 15777.2
DEMA DE MA DEEEE  -----> 1.07086
DEMA DE MA DEEEE  -----> 0.85712
DEMA DE MA DEEEE  -----> 1.6745
DEMA DE MA DEEEE  -----> 1.07086
DEMA DE MA DEEEE  -----> 1.95359
DEMA DE MA DEEEE  -----> 94.25700000000001   AUDJPY
DEMA DE MA DEEEE  -----> 86.98
DEMA DE MA DEEEE  -----> 94.25700000000001   AUDJPY
DEMA DE MA DEEEE  -----> 0.63949
 

It seems you have totally ignored William's first point. Here is some more information ...

Forum on trading, automated trading systems and testing trading strategies

MathRound fails for one particular number

Fernando Carreiro, 2018.01.01 22:08

He means that the value "0.69" cannot be exactly represented given the way a floating point number works (based on binary and not decimal representation) - hence, why the value gets represented as "0.68999999..." (see below).

You can never really "normalize" it and it is the main reason why both @whroeder1 and myself contest the use of the NormalizeDouble() function. It should in the very least be renamed to something like "RoundDigits()" because it is more of a "rounding" function and does not "normalize" in any way or fashion.


https://www.h-schmidt.net/FloatConverter/IEEE754.html

EDIT: Please note that the above images are for examples of representation in the 4-byte "float", and not the 8-byte "double" which offers more precision but still cannot represent the value "0.69" exactly due to the "binary" nature of the format.

EDIT2: For future readers that have difficulty understanding (or accepting) this concept, of decimal values not having an exact representation with a "float" or a "double", here is an article worth reading:

Why 0.1 Does Not Exist In Floating-Point

Many new programmers become aware of binary floating-point after seeing their programs give odd results: “Why does my program print 0.10000000000000001 when I enter 0.1?”; “Why does 0.3 + 0.6 = 0.89999999999999991?”; “Why does 6 * 0.1 not equal 0.6?” Questions like these are asked every day, on online forums like stackoverflow.com.

The answer is that most decimals have infinite representations in binary. Take 0.1 for example. It’s one of the simplest decimals you can think of, and yet it looks so complicated in binary:


Decimal 0.1 In Binary ( To 1369 Places

The bits go on forever; no matter how many of those bits you store in a computer, you will never end up with the binary equivalent of decimal 0.1.

... Read the rest of the article at: http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/

 
the usual common trouble with MQL double, which is not like other common programming language.

Be careful!
 
@Soewono Effendi #: the usual common trouble with MQL double, which is not like other common programming language. Be careful!

It is not an exclusive MQL problem. It is the same for ALL programming languages that use floating point.

You can see that by the links I provided and I quote from one of them ...

"The questions came from the context of a variety languages: Java, C++, C#, Python, Javascript, awk, Perl, Objective-C, and PHP. "

Floating-Point Questions Are Endless on stackoverflow.com
Floating-Point Questions Are Endless on stackoverflow.com
  • Rick Regan
  • www.exploringbinary.com
For years I’ve followed, through RSS, floating-point related questions on stackoverflow.com. Every day it seems there is a question like “why does 19.24 plus 6.95 equal 26.189999999999998?” I decided to track these questions, to see if my sense of their frequency was correct. I found that, in the last 40 days, there were 18 such questions...
 
In C or C++ after you rounded or floored a double variable to some digits (or precision if you want), you can safely expect the variable will remain as such. 
When you print or compare to.other double, you can safely expect to get correct result.

Good luck or should I say, be careful with MQL ;)
 
@Soewono Effendi #: In C or C++ after you rounded or floored a double variable to some digits (or precision if you want), you can safely expect the variable will remain as such. 
When you print or compare to.other double, you can safely expect to get correct result. Good luck or should I say, be careful with MQL ;)

That is not correct! In C or C++, rounding does not guarantee the exact number of digits. Floating point, has binary representation, not decimal representation.

Printed output, can be different to what is actually stored internally, especially if a set precision is defined for the output. So please, do not confuse the two.

Please read my post above and the links and proof provided.

 
Fernando Carreiro #:

That is not correct! In C or C++, rounding does not guarantee the exact number of digits. Floating point, has binary representation, not decimal representation.

Printed output, can be different to what is actually stored internally, especially if a set precision is defined for the output. So please, do not confuse the two.

Please read my post above and the links and proof provided.

OK, thanks for clarification.

Nevertheless, please be careful when using __ double __.
Always verify (double check) if the logic / math of your code as expected.

 
but the problem is not in mathematical way can normalize  it  , i know  is in binary is impossible but  when i transform it in string  i want truncate a string and i want  a string  with 7 digit , for insert in editbox , you tell me is impossible do also this ?
 
faustf #:
but the problem is not in mathematical way can normalize  it  , i know  is in binary is impossible but  when i transform it in string  i want truncate a string and i want  a string  with 7 digit , for insert in editbox , you tell me is impossible do also this ?

Use DoubleToStr() or StringFormat() and you'll be fine.

Reason: