Date un'occhiata a questo esempio:Visual Studio di debug vs. build di rilascio: il confronto int e float missmatch
#include <stdio.h>
int main() {
int i= 16777217;
float f = 16777216.0;
float g = i;
if(i == f)
printf("eq\n");
else
printf("neq\n");
if(g == f)
printf("eq\n");
else
printf("neq\n");
return 0;
}
che, utilizzando Visual Studio 2010 C++ (VS) in modalità di rilascio, gcc o g ++ (4.9 .2), ha l'uscita
eq
eq
che è ragionevole per me: Durante il primo confronto, i
viene implicitamente convertito ad un galleggiante dove viene troncato il bit significativo nella mantissa. Pertanto, sia i
sia f
hanno lo stesso schema di bit che si confronta con l'uguaglianza. Nel secondo if
si applica la stessa conversione, ma è già stata eseguita durante la definizione e l'inizializzazione di g
.
Tuttavia, utilizzando VS in modalità debug, il risultato è
neq
eq
Sembra, la conversione implicita durante confronto nella prima if
(come parte delle usuali conversioni aritmetiche in C e C++) non viene applicato . È vero? Esiste un meccanismo VS che impedisce tali falsi positivi nel confronto tra float e int (conversione in int/float con maggiore precisione)? Secondo MSDN VS C++ segue lo standard.
Ho controllato la rappresentazione di bit con this function. Per tutti i compilatori cede al
i = 00000001000000000000000000000001
f = 01001011100000000000000000000000
g = 01001011100000000000000000000000
float.h
su VS afferma #define FLT_MANT_DIG 24
in modo che il problema descritto troncamento dovrebbe valere pure.
Ho compilato tutti sulla stessa macchina (Intel i5-3570K) ma per VS in una scatola virtuale. La compilazione con VS su un'altra macchina stampa anche neq/eq
.
EDIT: codice assembler attaccato
differences_debug.asm
; Listing generated by Microsoft (R) Optimizing Compiler Version 16.00.30319.01
TITLE c:\Users\user\documents\visual studio 2010\Projects\differences\differences\differences.cpp
.686P
.XMM
include listing.inc
.model flat
INCLUDELIB MSVCRTD
INCLUDELIB OLDNAMES
PUBLIC [email protected][email protected][email protected] ; `string'
PUBLIC [email protected][email protected][email protected] ; `string'
PUBLIC [email protected]
PUBLIC _wmain
EXTRN __imp__printf:PROC
EXTRN __fltused:DWORD
EXTRN __RTC_CheckEsp:PROC
EXTRN __RTC_Shutdown:PROC
EXTRN __RTC_InitBase:PROC
; COMDAT [email protected][email protected][email protected]
; File c:\users\user\documents\visual studio 2010\projects\differences\differences\differences.cpp
CONST SEGMENT
[email protected][email protected][email protected] DB 'neq', 0aH, 00H ; `string'
CONST ENDS
; COMDAT [email protected][email protected][email protected]
CONST SEGMENT
[email protected][email protected][email protected] DB 'eq', 0aH, 00H ; `string'
CONST ENDS
; COMDAT [email protected]
CONST SEGMENT
[email protected] DD 04b800000r ; 1.67772e+007
CONST ENDS
; COMDAT rtc$TMZ
rtc$TMZ SEGMENT
__RTC_Shutdown.rtc$TMZ DD FLAT:__RTC_Shutdown
rtc$TMZ ENDS
; COMDAT rtc$IMZ
rtc$IMZ SEGMENT
__RTC_InitBase.rtc$IMZ DD FLAT:__RTC_InitBase
; Function compile flags: /Odtp /RTCsu /ZI
rtc$IMZ ENDS
; COMDAT _wmain
_TEXT SEGMENT
_g$ = -32 ; size = 4
_f$ = -20 ; size = 4
_i$ = -8 ; size = 4
_argc$ = 8 ; size = 4
_argv$ = 12 ; size = 4
_wmain PROC ; COMDAT
; Line 7
push ebp
mov ebp, esp
sub esp, 228 ; 000000e4H
push ebx
push esi
push edi
lea edi, DWORD PTR [ebp-228]
mov ecx, 57 ; 00000039H
mov eax, -858993460 ; ccccccccH
rep stosd
; Line 8
mov DWORD PTR _i$[ebp], 16777217 ; 01000001H
; Line 9
fld DWORD PTR [email protected]
fstp DWORD PTR _f$[ebp]
; Line 10
fild DWORD PTR _i$[ebp]
fstp DWORD PTR _g$[ebp]
; Line 13
fild DWORD PTR _i$[ebp]
fld DWORD PTR _f$[ebp]
fucompp
fnstsw ax
test ah, 68 ; 00000044H
jp SHORT [email protected]
; Line 14
mov esi, esp
push OFFSET [email protected][email protected][email protected]
call DWORD PTR __imp__printf
add esp, 4
cmp esi, esp
call __RTC_CheckEsp
; Line 15
jmp SHORT [email protected]
[email protected]:
; Line 16
mov esi, esp
push OFFSET [email protected][email protected][email protected]
call DWORD PTR __imp__printf
add esp, 4
cmp esi, esp
call __RTC_CheckEsp
[email protected]:
; Line 19
fld DWORD PTR _g$[ebp]
fld DWORD PTR _f$[ebp]
fucompp
fnstsw ax
test ah, 68 ; 00000044H
jp SHORT [email protected]
; Line 20
mov esi, esp
push OFFSET [email protected][email protected][email protected]
call DWORD PTR __imp__printf
add esp, 4
cmp esi, esp
call __RTC_CheckEsp
; Line 21
jmp SHORT [email protected]
[email protected]:
; Line 22
mov esi, esp
push OFFSET [email protected][email protected][email protected]
call DWORD PTR __imp__printf
add esp, 4
cmp esi, esp
call __RTC_CheckEsp
[email protected]:
; Line 24
xor eax, eax
; Line 26
pop edi
pop esi
pop ebx
add esp, 228 ; 000000e4H
cmp ebp, esp
call __RTC_CheckEsp
mov esp, ebp
pop ebp
ret 0
_wmain ENDP
_TEXT ENDS
END
differences_release.asm
; Listing generated by Microsoft (R) Optimizing Compiler Version 16.00.30319.01
TITLE c:\Users\user\documents\visual studio 2010\Projects\differences\differences\differences.cpp
.686P
.XMM
include listing.inc
.model flat
INCLUDELIB OLDNAMES
PUBLIC [email protected][email protected]?6?$[email protected] ; `string'
PUBLIC [email protected][email protected][email protected] ; `string'
EXTRN @[email protected]:PROC
EXTRN __imp__printf:PROC
; COMDAT [email protected][email protected][email protected]
CONST SEGMENT
[email protected][email protected][email protected] DB 'neq', 0aH, 00H ; `string'
CONST ENDS
; COMDAT [email protected][email protected][email protected]
CONST SEGMENT
[email protected][email protected][email protected] DB 'eq', 0aH, 00H ; `string'
CONST ENDS
PUBLIC _wmain
EXTRN __fltused:DWORD
; Function compile flags: /Ogtp
; COMDAT _wmain
_TEXT SEGMENT
_argc$ = 8 ; size = 4
_argv$ = 12 ; size = 4
_wmain PROC ; COMDAT
; File c:\users\user\documents\visual studio 2010\projects\differences\differences\differences.cpp
; Line 7
push esi
; Line 14
mov esi, DWORD PTR __imp__printf
push OFFSET [email protected][email protected][email protected]
call esi
; Line 20
push OFFSET [email protected][email protected][email protected]
call esi
add esp, 8
; Line 24
xor eax, eax
pop esi
; Line 26
ret 0
_wmain ENDP
_TEXT ENDS
END
Hai guardato il codice assembler generato? Forse dovresti postarlo qui. –
FYI compilato con VS2015 visualizza sempre 'eq eq' in modalità debug e release. –
Il debug VS potrebbe utilizzare FILD o FICOMP, che evita la conversione float int (funziona direttamente con i registri float 80b). L'opzione/fp potrebbe anche influenzare il comportamento. – Suma