Здравствуйте, Аноним, Вы писали:
А>Народ, почему когда в C# я пишу:
А>double w;
А>w=1/2;
А>после выполнения w=0.0?
в операдах выражения тип int — и результат приводится к типу int. Правильно так:
w = 1/2.0f;
Здравствуйте, <Аноним>, Вы писали:
А>Народ, почему когда в C# я пишу:
А>double w;
А>w=1/2;
потому что в целочисленной арифметике 1/2 == 0.

... << RSDN@Home 1.1.4 stable SR1 rev. 568>>
Здравствуйте, leleophan, Вы писали:
L>Здравствуйте, Аноним, Вы писали:
А>>Народ, почему когда в C# я пишу:
А>>double w;
А>>w=1/2;
А>>после выполнения w=0.0?
L>в операдах выражения тип int — и результат приводится к типу int. Правильно так:
L>L>w = 1/2.0f;
L>
Тогда уже так:
w = 1D / 2D;
Здравствуйте, Аноним, Вы писали:
А>Народ, почему когда в C# я пишу:
А>double w;
А>w=1/2;
А>после выполнения w=0.0?
Ребят, да что вы мучаетесь? w=0.5
Всё это даст 0,5
w = 1 / 2.0;
w = 1.0 / 2;
w = 1D / 2D;
11.1.5
If one of the operands is of an integral type, then that operand is converted to the floating-point type of
the other operand.
• Then, if either of the operands is of type double, the other operand is converted to double, the
operation is performed using at least double range and precision, and the type of the result is double
(or bool for the relational operators).
• Otherwise, the operation is performed using at least float range and precision, and the type of the
result is float (or bool for the relational operators).