[Issue 8476] New: float comparison operand not truncated from real
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Jul 30 14:28:28 PDT 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8476
Summary: float comparison operand not truncated from real
Product: D
Version: D2
Platform: x86
OS/Version: Linux
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: nobody at puremagic.com
ReportedBy: ellery-newcomer at utulsa.edu
--- Comment #0 from Ellery Newcomer <ellery-newcomer at utulsa.edu> 2012-07-30 14:28:26 PDT ---
The following code produces different results on a comparison.
The problem is in fitnessCompare; it looks like one of the two operands has
float precision, while the other has real precision.
assembly dump:
08065710
<_D19travelling_salesman4mainFZv14fitnessCompareMFS19travelling_salesman10chromosomeS19travelling_salesman10chromosomeZb>:
8065710: 55 push ebp
8065711: 8b ec mov ebp,esp
8065713: 83 ec 10 sub esp,0x10
8065716: ff 75 14 push DWORD PTR [ebp+0x14]
8065719: ff 75 10 push DWORD PTR [ebp+0x10]
806571c: e8 27 00 00 00 call 8065748
<_D19travelling_salesman7fitnessFNaxS19travelling_salesman10chromosomeZf>
8065721: ff 75 0c push DWORD PTR [ebp+0xc]
8065724: ff 75 08 push DWORD PTR [ebp+0x8]
8065727: d9 5d f0 fstp DWORD PTR [ebp-0x10]
806572a: e8 19 00 00 00 call 8065748
<_D19travelling_salesman7fitnessFNaxS19travelling_salesman10chromosomeZf>
806572f: d9 45 f0 fld DWORD PTR [ebp-0x10]
8065732: d9 c9 fxch st(1)
8065734: de d9 fcompp
8065736: df e0 fnstsw ax
8065738: 9e sahf
8065739: b8 01 00 00 00 mov eax,0x1
806573e: 7a 02 jp 8065742
<_D19travelling_salesman4mainFZv14fitnessCompareMFS19travelling_salesman10chromosomeS19travelling_salesman10chromosomeZb+0x32>
8065740: 72 02 jb 8065744
<_D19travelling_salesman4mainFZv14fitnessCompareMFS19travelling_salesman10chromosomeS19travelling_salesman10chromosomeZb+0x34>
8065742: 31 c0 xor eax,eax
8065744: c9 leave
8065745: c2 10 00 ret 0x10
actual code:
import std.stdio;
import std.random;
import std.array;
import std.math;
struct city{
int x;
int y;
}
struct chromosome{
city[] dna;
}
void main(){
bool fitnessCompare(chromosome first,chromosome second){
return fitness(first)>fitness(second);
}
auto less = &fitnessCompare;
auto z = chromosome([city(0, 10), city(25, 25), city(10, 65), city(50, 50),
city(75, 30), city(20, 0)]);
auto f = fitness(z);
writeln("fitness(z) > fitness(z) ?",f > f);
writeln("fitness(z) > fitness(z) ?",less(z,z));
}
float fitness(const chromosome victim) pure{
const city[] cities=city(0,0) ~ victim.dna ~ city(0,0);
//we need to start from home and return to home
float travelled=0f;
for(int x=0;x<cities.length-1;x++)
travelled+=distance(cities[x],cities[x+1]);
//writeln(100/travelled);
return 100/travelled;
}
float distance(city from,city to) pure{
return sqrt(cast(float)(pow(to.x-from.x,2) + pow(to.y-from.y,2)));
}
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list