Bug 3928 – (D1 only) Comparing imaginaries with reals produces results that are inconsistent
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2010-03-10T12:04:36Z
Last change time
2019-08-12T06:05:08Z
Keywords
accepts-invalid, spec, wrong-code
Assigned to
No Owner
Creator
Aldo Nunez
Comments
Comment #0 by aldonunez1 — 2010-03-10T12:04:36Z
Comparing an imaginary number like an ifloat with a real one like a float:
1. Has results that differ at compile time and runtime.
2. At runtime is done in the wrong order.
3. Doesn't seem to be very meaningful, given that you can't compare complex numbers.
With respect to each issue above, here are some more details.
1. Different results at runtime and compile time.
At compile time, if the left is imaginary, then we take the imaginary part of the right side and compare the two. If the left side is real, then we take the real part of the right side and compare.
All of this is as seen in constfold.c.
At runtime, we turn both sides into complex numbers by making a missing part zero. Then, we compare the imaginary parts, and if they're equal, compare the real parts.
2. Wrong order at runtime.
In the runtime comparison described in #1 what's actually compared is right to left instead of left to right. If my program has "a < b", then "b < a" is checked.
3. Meaning?
Comparisons where at least one side is a complex number are not allowed in D. As such, is it meaningful to allow the comparisons described here, given that such numbers are treated as complex numbers and compared?
Note, all of this is strictly where one side of a compare expression is an imaginary number and the other is a real number.
Here's a sample program:
import std.stdio;
void main()
{
cfloat cf1 = 1 + 2i;
cfloat cf2 = 3 + 4i;
ifloat if2 = 1i;
float f2 = 3;
ifloat if3 = 4i;
float f3 = 2;
ifloat if4 = 5i;
float f4 = 5;
writeln( "--- < ---" );
writeln( if2 < f2 );
writeln( f2 < if2 );
writeln( if3 < f3 );
writeln( f3 < if3 );
writeln( if4 < f4 );
writeln( f4 < if4 );
writeln( 1i < 3 );
writeln( 3 < 1i );
writeln( 4i < 2 );
writeln( 2 < 4i );
writeln( 5i < 5 );
writeln( 5 < 5i );
writeln( "--- == ---" );
writeln( if2 == f2 );
writeln( f2 == if2 );
writeln( if3 == f3 );
writeln( f3 == if3 );
writeln( if4 == f4 );
writeln( f4 == if4 );
writeln( 1i == 3 );
writeln( 3 == 1i );
writeln( 4i == 2 );
writeln( 2 == 4i );
writeln( 5i == 5 );
writeln( 5 == 5i );
writeln( "--- > ---" );
writeln( if2 > f2 );
writeln( f2 > if2 );
writeln( if3 > f3 );
writeln( f3 > if3 );
writeln( if4 > f4 );
writeln( f4 > if4 );
writeln( 1i > 3 );
writeln( 3 > 1i );
writeln( 4i > 2 );
writeln( 2 > 4i );
writeln( 5i > 5 );
writeln( 5 > 5i );
}
Comment #1 by clugdbug — 2010-03-14T13:47:03Z
It's simpler than that. Comparisons between a pure imaginary and a pure real are nonsense, and shouldn't compile.
Comment #2 by bugzilla — 2012-01-23T15:43:39Z
Built-in complex numbers are deprecated anyway for D2, so redone as a D1 only issue.
Comment #3 by pro.mathias.lang — 2019-08-12T06:05:08Z