D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3928 - (D1 only) Comparing imaginaries with reals produces results that are inconsistent
Summary: (D1 only) Comparing imaginaries with reals produces results that are inconsis...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 All
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid, spec, wrong-code
Depends on:
Blocks:
 
Reported: 2010-03-10 12:04 UTC by Aldo Nunez
Modified: 2019-08-12 06:05 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Aldo Nunez 2010-03-10 12:04:36 UTC
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 Don 2010-03-14 13:47:03 UTC
It's simpler than that. Comparisons between a pure imaginary and a pure real are nonsense, and shouldn't compile.
Comment 2 Walter Bright 2012-01-23 15:43:39 UTC
Built-in complex numbers are deprecated anyway for D2, so redone as a D1 only issue.
Comment 3 Mathias LANG 2019-08-12 06:05:08 UTC
And D1 is gone