Error in code calculation

jmh530 john.michael.hall at gmail.com
Thu Apr 29 17:26:29 UTC 2021


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)`.


More information about the Digitalmars-d mailing list