www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Error in code calculation

reply Sergei <kls tut.by> writes:
When raising to a power of a fractional number, an incorrect 
subtraction occurs:

import std.stdio;

void main()
{
     float x;
         x=1/2;
     writeln (4^^x);
     writeln (4^^(1/2));
}
Apr 29
parent reply jmh530 <john.michael.hall gmail.com> writes:
On Thursday, 29 April 2021 at 17:17:11 UTC, Sergei wrote:
 When raising to a power of a fractional number, an incorrect 
 subtraction occurs:

 import std.stdio;

 void main()
 {
     float x;
         x=1/2;
     writeln (4^^x);
     writeln (4^^(1/2));
 }
Probably more approach for Learn. Your problem becomes apparent if you `writeln(x)`. The calculation does integer division (1 goes 0 times into 2) and then assigns that to a float. What you want is `x=1.0/2;` and `4^^(1.0/2)`.
Apr 29
parent jmh530 <john.michael.hall gmail.com> writes:
On Thursday, 29 April 2021 at 17:26:29 UTC, jmh530 wrote:
 [snip[

 Probably more approach for Learn.
Probably more appropriate for Learn.
Apr 29