comparing a #define constant with a variable of same value does not equal (C language) [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
strange output in comparison of float with float literal
8 answers
I am new to C language. Here is my question and code:
I have a constant and a variable with the same value and I try to compare them to check if they are equal. It seems to me that since they are assigned the same value, they should be equal but that is not the case.
#include <stdio.h>
#define mypi1 3.14
int main()
{
int ans;
float mypi2 = 3.14;
if(mypi1==mypi2)
{
ans=1;
}
else
{
ans=0;
}
printf("%i",ans);
return 0;
}
My output is 0. Which indicates they are not equal. What is the reasoning behind this? This is a really simple question but I could not find it anywhere. Please help and thanks in advance.
c
marked as duplicate by Swordfish, M.M
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
strange output in comparison of float with float literal
8 answers
I am new to C language. Here is my question and code:
I have a constant and a variable with the same value and I try to compare them to check if they are equal. It seems to me that since they are assigned the same value, they should be equal but that is not the case.
#include <stdio.h>
#define mypi1 3.14
int main()
{
int ans;
float mypi2 = 3.14;
if(mypi1==mypi2)
{
ans=1;
}
else
{
ans=0;
}
printf("%i",ans);
return 0;
}
My output is 0. Which indicates they are not equal. What is the reasoning behind this? This is a really simple question but I could not find it anywhere. Please help and thanks in advance.
c
marked as duplicate by Swordfish, M.M
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
3.14
is a double,mypi2
is afloat
.
– Swordfish
Nov 22 '18 at 0:34
Hint: replace the definition ofmypi1
with3.14f
and observe the output of the program
– Govind Parmar
Nov 22 '18 at 0:35
add a comment |
This question already has an answer here:
strange output in comparison of float with float literal
8 answers
I am new to C language. Here is my question and code:
I have a constant and a variable with the same value and I try to compare them to check if they are equal. It seems to me that since they are assigned the same value, they should be equal but that is not the case.
#include <stdio.h>
#define mypi1 3.14
int main()
{
int ans;
float mypi2 = 3.14;
if(mypi1==mypi2)
{
ans=1;
}
else
{
ans=0;
}
printf("%i",ans);
return 0;
}
My output is 0. Which indicates they are not equal. What is the reasoning behind this? This is a really simple question but I could not find it anywhere. Please help and thanks in advance.
c
This question already has an answer here:
strange output in comparison of float with float literal
8 answers
I am new to C language. Here is my question and code:
I have a constant and a variable with the same value and I try to compare them to check if they are equal. It seems to me that since they are assigned the same value, they should be equal but that is not the case.
#include <stdio.h>
#define mypi1 3.14
int main()
{
int ans;
float mypi2 = 3.14;
if(mypi1==mypi2)
{
ans=1;
}
else
{
ans=0;
}
printf("%i",ans);
return 0;
}
My output is 0. Which indicates they are not equal. What is the reasoning behind this? This is a really simple question but I could not find it anywhere. Please help and thanks in advance.
This question already has an answer here:
strange output in comparison of float with float literal
8 answers
c
c
asked Nov 22 '18 at 0:32
lollalollalollalolla
82
82
marked as duplicate by Swordfish, M.M
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Swordfish, M.M
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 0:36
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
3.14
is a double,mypi2
is afloat
.
– Swordfish
Nov 22 '18 at 0:34
Hint: replace the definition ofmypi1
with3.14f
and observe the output of the program
– Govind Parmar
Nov 22 '18 at 0:35
add a comment |
1
3.14
is a double,mypi2
is afloat
.
– Swordfish
Nov 22 '18 at 0:34
Hint: replace the definition ofmypi1
with3.14f
and observe the output of the program
– Govind Parmar
Nov 22 '18 at 0:35
1
1
3.14
is a double, mypi2
is a float
.– Swordfish
Nov 22 '18 at 0:34
3.14
is a double, mypi2
is a float
.– Swordfish
Nov 22 '18 at 0:34
Hint: replace the definition of
mypi1
with 3.14f
and observe the output of the program– Govind Parmar
Nov 22 '18 at 0:35
Hint: replace the definition of
mypi1
with 3.14f
and observe the output of the program– Govind Parmar
Nov 22 '18 at 0:35
add a comment |
1 Answer
1
active
oldest
votes
#define mypi1 3.14
float mypi2 = 3.14;
The first of those is a double
type, the second is a double
coerced into a float
.
The expression mypi1==mypi2
will first convert the float
back to a double
before comparing (the idea is that, if one type is of lesser range/precision than the other, it's converted so that both types are identical).
Hence, if the if
statement is failing, it's likely that you lose information in the double -> float -> double
round-trip(a).
To be honest, unless you're using a great many floating point values (and storage space is a concern), you should probably just use double
everywhere. If you do need float
types, use that for both values:
#define mypi1 3.14f
float mypi2 = 3.14f;
Comparing two float
variables will not involve any conversions.
(a) See, for example, the following complete program:
#include <stdio.h>
#define x 3.14
int main(void) {
float y = 3.14; // double -> float
double z = y; // -> double
printf("%.50fn", x);
printf("%.50fn", z);
}
In this, x
is a double
and z
is a double
that's gone through the round-trip conversion discussed above. The output shows the difference that can happen:
3.14000000000000012434497875801753252744674682617188
3.14000010490417480468750000000000000000000000000000
This is very clear, thanks. However, I tried changing the value to 10.5 and the output return is 1 which indicates they are identical. Could you please explain this?
– lollalolla
Nov 22 '18 at 1:06
@lollalolla: See Is floating point math broken?. Succinctly, there's an exact binary representation of 10.5 that bothfloat
anddouble
can store; there isn't an exact binary representation of 3.14 (period), andfloat
anddouble
can only store different approximations to it (because they have different numbers of bits to store the value), and when thefloat
is converted todouble
, what it stored isn't the same as what thedouble
stored from the start.
– Jonathan Leffler
Nov 22 '18 at 2:44
@lollalolla, Jonathan is correct. Basically, numbers that can be built from constituents that are powers of two within the bit precision of the type (such as2^3 + 2^1 + 2^-1 = 8 + 2 + 0.5 = 10.5
) can be represented exactly. For3.14
, you can get3
from2 + 1
but the0.14
bit is problematic.
– paxdiablo
Nov 22 '18 at 6:24
Thanks all. Now it makes perfect sense to me!
– lollalolla
Nov 22 '18 at 12:45
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
#define mypi1 3.14
float mypi2 = 3.14;
The first of those is a double
type, the second is a double
coerced into a float
.
The expression mypi1==mypi2
will first convert the float
back to a double
before comparing (the idea is that, if one type is of lesser range/precision than the other, it's converted so that both types are identical).
Hence, if the if
statement is failing, it's likely that you lose information in the double -> float -> double
round-trip(a).
To be honest, unless you're using a great many floating point values (and storage space is a concern), you should probably just use double
everywhere. If you do need float
types, use that for both values:
#define mypi1 3.14f
float mypi2 = 3.14f;
Comparing two float
variables will not involve any conversions.
(a) See, for example, the following complete program:
#include <stdio.h>
#define x 3.14
int main(void) {
float y = 3.14; // double -> float
double z = y; // -> double
printf("%.50fn", x);
printf("%.50fn", z);
}
In this, x
is a double
and z
is a double
that's gone through the round-trip conversion discussed above. The output shows the difference that can happen:
3.14000000000000012434497875801753252744674682617188
3.14000010490417480468750000000000000000000000000000
This is very clear, thanks. However, I tried changing the value to 10.5 and the output return is 1 which indicates they are identical. Could you please explain this?
– lollalolla
Nov 22 '18 at 1:06
@lollalolla: See Is floating point math broken?. Succinctly, there's an exact binary representation of 10.5 that bothfloat
anddouble
can store; there isn't an exact binary representation of 3.14 (period), andfloat
anddouble
can only store different approximations to it (because they have different numbers of bits to store the value), and when thefloat
is converted todouble
, what it stored isn't the same as what thedouble
stored from the start.
– Jonathan Leffler
Nov 22 '18 at 2:44
@lollalolla, Jonathan is correct. Basically, numbers that can be built from constituents that are powers of two within the bit precision of the type (such as2^3 + 2^1 + 2^-1 = 8 + 2 + 0.5 = 10.5
) can be represented exactly. For3.14
, you can get3
from2 + 1
but the0.14
bit is problematic.
– paxdiablo
Nov 22 '18 at 6:24
Thanks all. Now it makes perfect sense to me!
– lollalolla
Nov 22 '18 at 12:45
add a comment |
#define mypi1 3.14
float mypi2 = 3.14;
The first of those is a double
type, the second is a double
coerced into a float
.
The expression mypi1==mypi2
will first convert the float
back to a double
before comparing (the idea is that, if one type is of lesser range/precision than the other, it's converted so that both types are identical).
Hence, if the if
statement is failing, it's likely that you lose information in the double -> float -> double
round-trip(a).
To be honest, unless you're using a great many floating point values (and storage space is a concern), you should probably just use double
everywhere. If you do need float
types, use that for both values:
#define mypi1 3.14f
float mypi2 = 3.14f;
Comparing two float
variables will not involve any conversions.
(a) See, for example, the following complete program:
#include <stdio.h>
#define x 3.14
int main(void) {
float y = 3.14; // double -> float
double z = y; // -> double
printf("%.50fn", x);
printf("%.50fn", z);
}
In this, x
is a double
and z
is a double
that's gone through the round-trip conversion discussed above. The output shows the difference that can happen:
3.14000000000000012434497875801753252744674682617188
3.14000010490417480468750000000000000000000000000000
This is very clear, thanks. However, I tried changing the value to 10.5 and the output return is 1 which indicates they are identical. Could you please explain this?
– lollalolla
Nov 22 '18 at 1:06
@lollalolla: See Is floating point math broken?. Succinctly, there's an exact binary representation of 10.5 that bothfloat
anddouble
can store; there isn't an exact binary representation of 3.14 (period), andfloat
anddouble
can only store different approximations to it (because they have different numbers of bits to store the value), and when thefloat
is converted todouble
, what it stored isn't the same as what thedouble
stored from the start.
– Jonathan Leffler
Nov 22 '18 at 2:44
@lollalolla, Jonathan is correct. Basically, numbers that can be built from constituents that are powers of two within the bit precision of the type (such as2^3 + 2^1 + 2^-1 = 8 + 2 + 0.5 = 10.5
) can be represented exactly. For3.14
, you can get3
from2 + 1
but the0.14
bit is problematic.
– paxdiablo
Nov 22 '18 at 6:24
Thanks all. Now it makes perfect sense to me!
– lollalolla
Nov 22 '18 at 12:45
add a comment |
#define mypi1 3.14
float mypi2 = 3.14;
The first of those is a double
type, the second is a double
coerced into a float
.
The expression mypi1==mypi2
will first convert the float
back to a double
before comparing (the idea is that, if one type is of lesser range/precision than the other, it's converted so that both types are identical).
Hence, if the if
statement is failing, it's likely that you lose information in the double -> float -> double
round-trip(a).
To be honest, unless you're using a great many floating point values (and storage space is a concern), you should probably just use double
everywhere. If you do need float
types, use that for both values:
#define mypi1 3.14f
float mypi2 = 3.14f;
Comparing two float
variables will not involve any conversions.
(a) See, for example, the following complete program:
#include <stdio.h>
#define x 3.14
int main(void) {
float y = 3.14; // double -> float
double z = y; // -> double
printf("%.50fn", x);
printf("%.50fn", z);
}
In this, x
is a double
and z
is a double
that's gone through the round-trip conversion discussed above. The output shows the difference that can happen:
3.14000000000000012434497875801753252744674682617188
3.14000010490417480468750000000000000000000000000000
#define mypi1 3.14
float mypi2 = 3.14;
The first of those is a double
type, the second is a double
coerced into a float
.
The expression mypi1==mypi2
will first convert the float
back to a double
before comparing (the idea is that, if one type is of lesser range/precision than the other, it's converted so that both types are identical).
Hence, if the if
statement is failing, it's likely that you lose information in the double -> float -> double
round-trip(a).
To be honest, unless you're using a great many floating point values (and storage space is a concern), you should probably just use double
everywhere. If you do need float
types, use that for both values:
#define mypi1 3.14f
float mypi2 = 3.14f;
Comparing two float
variables will not involve any conversions.
(a) See, for example, the following complete program:
#include <stdio.h>
#define x 3.14
int main(void) {
float y = 3.14; // double -> float
double z = y; // -> double
printf("%.50fn", x);
printf("%.50fn", z);
}
In this, x
is a double
and z
is a double
that's gone through the round-trip conversion discussed above. The output shows the difference that can happen:
3.14000000000000012434497875801753252744674682617188
3.14000010490417480468750000000000000000000000000000
edited Nov 22 '18 at 0:43
answered Nov 22 '18 at 0:36
paxdiablopaxdiablo
643k17712681689
643k17712681689
This is very clear, thanks. However, I tried changing the value to 10.5 and the output return is 1 which indicates they are identical. Could you please explain this?
– lollalolla
Nov 22 '18 at 1:06
@lollalolla: See Is floating point math broken?. Succinctly, there's an exact binary representation of 10.5 that bothfloat
anddouble
can store; there isn't an exact binary representation of 3.14 (period), andfloat
anddouble
can only store different approximations to it (because they have different numbers of bits to store the value), and when thefloat
is converted todouble
, what it stored isn't the same as what thedouble
stored from the start.
– Jonathan Leffler
Nov 22 '18 at 2:44
@lollalolla, Jonathan is correct. Basically, numbers that can be built from constituents that are powers of two within the bit precision of the type (such as2^3 + 2^1 + 2^-1 = 8 + 2 + 0.5 = 10.5
) can be represented exactly. For3.14
, you can get3
from2 + 1
but the0.14
bit is problematic.
– paxdiablo
Nov 22 '18 at 6:24
Thanks all. Now it makes perfect sense to me!
– lollalolla
Nov 22 '18 at 12:45
add a comment |
This is very clear, thanks. However, I tried changing the value to 10.5 and the output return is 1 which indicates they are identical. Could you please explain this?
– lollalolla
Nov 22 '18 at 1:06
@lollalolla: See Is floating point math broken?. Succinctly, there's an exact binary representation of 10.5 that bothfloat
anddouble
can store; there isn't an exact binary representation of 3.14 (period), andfloat
anddouble
can only store different approximations to it (because they have different numbers of bits to store the value), and when thefloat
is converted todouble
, what it stored isn't the same as what thedouble
stored from the start.
– Jonathan Leffler
Nov 22 '18 at 2:44
@lollalolla, Jonathan is correct. Basically, numbers that can be built from constituents that are powers of two within the bit precision of the type (such as2^3 + 2^1 + 2^-1 = 8 + 2 + 0.5 = 10.5
) can be represented exactly. For3.14
, you can get3
from2 + 1
but the0.14
bit is problematic.
– paxdiablo
Nov 22 '18 at 6:24
Thanks all. Now it makes perfect sense to me!
– lollalolla
Nov 22 '18 at 12:45
This is very clear, thanks. However, I tried changing the value to 10.5 and the output return is 1 which indicates they are identical. Could you please explain this?
– lollalolla
Nov 22 '18 at 1:06
This is very clear, thanks. However, I tried changing the value to 10.5 and the output return is 1 which indicates they are identical. Could you please explain this?
– lollalolla
Nov 22 '18 at 1:06
@lollalolla: See Is floating point math broken?. Succinctly, there's an exact binary representation of 10.5 that both
float
and double
can store; there isn't an exact binary representation of 3.14 (period), and float
and double
can only store different approximations to it (because they have different numbers of bits to store the value), and when the float
is converted to double
, what it stored isn't the same as what the double
stored from the start.– Jonathan Leffler
Nov 22 '18 at 2:44
@lollalolla: See Is floating point math broken?. Succinctly, there's an exact binary representation of 10.5 that both
float
and double
can store; there isn't an exact binary representation of 3.14 (period), and float
and double
can only store different approximations to it (because they have different numbers of bits to store the value), and when the float
is converted to double
, what it stored isn't the same as what the double
stored from the start.– Jonathan Leffler
Nov 22 '18 at 2:44
@lollalolla, Jonathan is correct. Basically, numbers that can be built from constituents that are powers of two within the bit precision of the type (such as
2^3 + 2^1 + 2^-1 = 8 + 2 + 0.5 = 10.5
) can be represented exactly. For 3.14
, you can get 3
from 2 + 1
but the 0.14
bit is problematic.– paxdiablo
Nov 22 '18 at 6:24
@lollalolla, Jonathan is correct. Basically, numbers that can be built from constituents that are powers of two within the bit precision of the type (such as
2^3 + 2^1 + 2^-1 = 8 + 2 + 0.5 = 10.5
) can be represented exactly. For 3.14
, you can get 3
from 2 + 1
but the 0.14
bit is problematic.– paxdiablo
Nov 22 '18 at 6:24
Thanks all. Now it makes perfect sense to me!
– lollalolla
Nov 22 '18 at 12:45
Thanks all. Now it makes perfect sense to me!
– lollalolla
Nov 22 '18 at 12:45
add a comment |
1
3.14
is a double,mypi2
is afloat
.– Swordfish
Nov 22 '18 at 0:34
Hint: replace the definition of
mypi1
with3.14f
and observe the output of the program– Govind Parmar
Nov 22 '18 at 0:35