NormalizeDouble paradox - page 11

 
transcendreamer:

What bothers me is that when I calculate a fractional number, normalise it, write it into a variable, then read and normalise it again, I get tails

e.g.

basis[0]=NormalizeDouble(sum_A,2);

GlobalVariableSet("Equity-"+portfolio_id,basis[0]); 

...

current=NormalizeDouble(GlobalVariableGet("Equity-"+portfolio_id),2);

text = "Positions synchronized at " + current + " for portfolio: " + portfolio_name;

if(!automatic) MessageBox(text,""); else Print(text);

Now I've changed it to

text = "Positions synchronized at " + DoubleToStr(current,2) + " for portfolio: " + portfolio_name;

if(!automatic) MessageBox(text,""); else Print(text);

and there do not seem to be any tails, whew, whew, whew...

I've already written, I'll repeat - some numbers don't exist in binary. There is no 0.1, no 0.3 and many others. No matter how many times you write double val = NormalizeDouble(0.1434, 1), you will never get 0.1 just because there is no such a number.

For example, numbers are represented as follows:

0.1 = 0.100000000000000000001
0.2 = 0.20000000000000001
0.3 = 0.29999999999999999999999
0.4 = 0.4000000000000000002
0.6 = 0.5999999999999999998

but
0.125 = 0.125
0.25 = 0.25
0.5 = 0.5

If this is very disturbing, you either need to round or write your own bicycle to represent fractional numbers (there may be some libraries, not interested) which won't use fpu.

And no tails are added during conversion to string, they are there initially.

 
transcendreamer:

so I've solved my question, we can close the subject

solution: you have to force rounding, even after normalisation

Wrong solution. When rounding, you have to multiply, round, divide. After the last division, the number will be unnormalized.
 
transcendreamer:

quite possibly... but in terms of the end result - there are tails!

...

No tails after normalisation.
 
Integer:
There are no tails after normalize.

I think you are misleading the man. What do you mean there are no tails after normalization? There can be no tails only in the string that we got from a number and rounded the value (rounded already in the string, not in double). But there are tails after NormalizeDouble().

 
Integer:
Wrong solution. When rounding, you have to multiply, round, divide. After the last division the number will be non-normalized.

What do you mean by normalisation? Stringo was saying here that the algorithm is something like this:

double NormalizeDouble(double val, int digits)
{
    val *= 10 ^ digits
    округляем к ближайшему целому
    val /= 10 ^ digits
    return val
}
 
pavlick_:

I think you are misleading the man. What do you mean there are no tails after normalization? There can be no tails only in the string that we got from a number and rounded the value (rounded already in the string, not in double). But after NormalizeDouble() there are tails.

Well, if you like to believe that I am misleading someone, believe me. I've seen all sorts of people here, I will not be surprised by your "principled" position.
 
pavlick_:

What do you mean by normalisation? Stringo was saying here that the algorithm is something like this:

It would be much more interesting what you mean by normalisation.
 
Integer:
I'm asking you a specific question and you're pouring water on me. It's kind of normal to explain and argue your point of view.
 
pavlick_:
I'm asking you a specific question and you're pouring water on me. How is it normal to explain and argue your point of view?
Read the textbooks and documentation aloud, or what? Or read the dictionary aloud, about the word "approximately"?
 
Integer:
Read the textbooks and documentation aloud or what? Or read the dictionary aloud, about the word "approximately"?
It's clear, we have nothing to talk about. And you'd better stay out of conversations if you're not responsible for your words.
Reason: