You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
NormalizeDouble
Rounding floating point number to a specified accuracy.
double NormalizeDouble(
double value, // normalized number
int digits // number of digits after decimal point
);
When rounding the float point number with a specified accuracy of 2 digits results in abnormal behavior
Hi everybody,
I've notice the same thing, and I also wanted MathCeil and MathFloor for double numbers, I've redesigned these functions and MathRound in my code for that :
That works very well, with the price of at least twice the time of NormalizeDouble execution
If this can help someone
That works very well, with the price of at least twice the time of NormalizeDouble execution
If this can help someone
Exact, thanks,
I just tested your examples with a NormalizeDouble just before the function on integer like that :
the time for this plus is insignificant
It seems ok, what do you think ?
Exact, thanks,
I just tested your examples with a NormalizeDouble just before the function on integer like that :
the time for this plus is insignificant
It seems ok, what do you think ?
No, unfortunately. For example: MathFloor(2.157, 2) => 2.16 which is wrong, it should be 2.15.
Please see my library for precise functions for rounding. https://www.mql5.com/en/code/20822
(Actually, these are not mandatory for rounding prices for trading orders, but simple functions are Ok).
I am sending you a script to compare how your functions perform against Microsoft .Net library (the most precise for decimal rounding, as it uses the 128-bit decimal type).
Edit: I re-uploaded the files due to some mistakes in the code. Also, please install "Ron's CSV Edit" on your computer to open files\result.csv automatically.Ok, I've seen your math utils library and tried your function with mathlog10 etc in a loop, and tried too with just DBL_DIG for my NormalizeDouble digit,
I think mine with DBL_DIG is more than enough, 13ms for some 1 million loop with some mathrand, mathround, conditions, comparisons, it's twice the time earlier,
The same loop with yours take 56ms, I do a so complex EA with a lot of calculations, I don't do any round if it's not necessary but I can't accept this difference,
I do NormalyzeDouble the value * 10power at DBL_DIG, and added or substract FLT_EPSILON, like I do for comparisons, for compute the MathFunction correctly and then divide by 10power and seems ok with all of your examples and other like floor 2.149999 etc
Thank you a lot for your contribution, It has helped me so well to do something the most exact like possible.. Look like this now : (two MathPow don't take any longer than int in RAM, I tried DBL_EPSILON, that does not work)
If you look for speed + accuracy:
But, this works for POSITIVE numbers, only.
I thing your multiple 1 plus DBL_EPSILON is better for accuracy with higher digits than 6, but i don't need that
I do a simple little loop to extract the digit integer value of volume step, maybe there is a better way, I don't have thought about that for a time
If you look for speed + accuracy:
But, this works for POSITIVE numbers, only.
Add a little condition
This does not add much more time
I've seen your deleted answer where you mentioned MathAbs(MathLog10(step)), thanks, my math is old, I did not remember what was log10
Nice. Thanks!
Now, it works with positive or negative numbers. I used the RoundSharp .Net library to verify its correctness.
I have saved it to my library like that:
Note, use negative digits to round to tens and hundreds. MathRound(1327, -2) => 1300
Edit: updated code + attached newer verification script.