Whenever I enter 4.2 in this code, the value of nm is 19, where 20 is expected
up vote
1
down vote
favorite
I am trying to get 20 in the variable nm. But it is returning 19.
How do I fix it? And what is causing this problem?
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float m;
int n = 0;
do {
m = get_float("Enter:");
}while(m < 0);
int nm =(m * 100 );
while(nm >= 25 ){
nm = nm - 25;
n = n + 1;
}
printf("%in",n);
printf("%in", nm);
}
c cs50
add a comment |
up vote
1
down vote
favorite
I am trying to get 20 in the variable nm. But it is returning 19.
How do I fix it? And what is causing this problem?
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float m;
int n = 0;
do {
m = get_float("Enter:");
}while(m < 0);
int nm =(m * 100 );
while(nm >= 25 ){
nm = nm - 25;
n = n + 1;
}
printf("%in",n);
printf("%in", nm);
}
c cs50
1
And that, kids, is why we don't ever rely on transitive equality when pushing a float through anint
.
– WhozCraig
Nov 19 at 19:19
1
It's very tempting just to close this as a duplicate of Is floating point math broken?
– Jonathan Leffler
Nov 19 at 19:57
Possible duplicate of Is floating point math broken?
– Sean Pianka
Nov 19 at 20:50
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to get 20 in the variable nm. But it is returning 19.
How do I fix it? And what is causing this problem?
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float m;
int n = 0;
do {
m = get_float("Enter:");
}while(m < 0);
int nm =(m * 100 );
while(nm >= 25 ){
nm = nm - 25;
n = n + 1;
}
printf("%in",n);
printf("%in", nm);
}
c cs50
I am trying to get 20 in the variable nm. But it is returning 19.
How do I fix it? And what is causing this problem?
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float m;
int n = 0;
do {
m = get_float("Enter:");
}while(m < 0);
int nm =(m * 100 );
while(nm >= 25 ){
nm = nm - 25;
n = n + 1;
}
printf("%in",n);
printf("%in", nm);
}
c cs50
c cs50
edited Nov 28 at 7:05
Jonathan Leffler
556k886621015
556k886621015
asked Nov 19 at 19:16
I M Hirobumi
84
84
1
And that, kids, is why we don't ever rely on transitive equality when pushing a float through anint
.
– WhozCraig
Nov 19 at 19:19
1
It's very tempting just to close this as a duplicate of Is floating point math broken?
– Jonathan Leffler
Nov 19 at 19:57
Possible duplicate of Is floating point math broken?
– Sean Pianka
Nov 19 at 20:50
add a comment |
1
And that, kids, is why we don't ever rely on transitive equality when pushing a float through anint
.
– WhozCraig
Nov 19 at 19:19
1
It's very tempting just to close this as a duplicate of Is floating point math broken?
– Jonathan Leffler
Nov 19 at 19:57
Possible duplicate of Is floating point math broken?
– Sean Pianka
Nov 19 at 20:50
1
1
And that, kids, is why we don't ever rely on transitive equality when pushing a float through an
int
.– WhozCraig
Nov 19 at 19:19
And that, kids, is why we don't ever rely on transitive equality when pushing a float through an
int
.– WhozCraig
Nov 19 at 19:19
1
1
It's very tempting just to close this as a duplicate of Is floating point math broken?
– Jonathan Leffler
Nov 19 at 19:57
It's very tempting just to close this as a duplicate of Is floating point math broken?
– Jonathan Leffler
Nov 19 at 19:57
Possible duplicate of Is floating point math broken?
– Sean Pianka
Nov 19 at 20:50
Possible duplicate of Is floating point math broken?
– Sean Pianka
Nov 19 at 20:50
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
A 32-bit float
can encode about 232 different values exactly. Due to the binary nature of the typical float, 4.2 is not one of them.
Instead m
has the value of 4.1999998...
// closest float
4.19999980926513671875
// hoped for value
4.2
// next closest float
4.200000286102294921875
Multiplexing by 100 incurs some rounding and the best answer to m*100
is then 419.999969482421875
.
int nm =(m * 100 );
results nm == 419
as assignment of a float
to int
truncates the fractional portion away.
Consider rounding to the nearest integer rather than truncating via int
assignment and use double
constants.
#include <math.h>
// int nm =(m * 100 );
int nm = lround(m * 100.0);
// or
int nm = round(m * 100.0);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
A 32-bit float
can encode about 232 different values exactly. Due to the binary nature of the typical float, 4.2 is not one of them.
Instead m
has the value of 4.1999998...
// closest float
4.19999980926513671875
// hoped for value
4.2
// next closest float
4.200000286102294921875
Multiplexing by 100 incurs some rounding and the best answer to m*100
is then 419.999969482421875
.
int nm =(m * 100 );
results nm == 419
as assignment of a float
to int
truncates the fractional portion away.
Consider rounding to the nearest integer rather than truncating via int
assignment and use double
constants.
#include <math.h>
// int nm =(m * 100 );
int nm = lround(m * 100.0);
// or
int nm = round(m * 100.0);
add a comment |
up vote
1
down vote
accepted
A 32-bit float
can encode about 232 different values exactly. Due to the binary nature of the typical float, 4.2 is not one of them.
Instead m
has the value of 4.1999998...
// closest float
4.19999980926513671875
// hoped for value
4.2
// next closest float
4.200000286102294921875
Multiplexing by 100 incurs some rounding and the best answer to m*100
is then 419.999969482421875
.
int nm =(m * 100 );
results nm == 419
as assignment of a float
to int
truncates the fractional portion away.
Consider rounding to the nearest integer rather than truncating via int
assignment and use double
constants.
#include <math.h>
// int nm =(m * 100 );
int nm = lround(m * 100.0);
// or
int nm = round(m * 100.0);
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
A 32-bit float
can encode about 232 different values exactly. Due to the binary nature of the typical float, 4.2 is not one of them.
Instead m
has the value of 4.1999998...
// closest float
4.19999980926513671875
// hoped for value
4.2
// next closest float
4.200000286102294921875
Multiplexing by 100 incurs some rounding and the best answer to m*100
is then 419.999969482421875
.
int nm =(m * 100 );
results nm == 419
as assignment of a float
to int
truncates the fractional portion away.
Consider rounding to the nearest integer rather than truncating via int
assignment and use double
constants.
#include <math.h>
// int nm =(m * 100 );
int nm = lround(m * 100.0);
// or
int nm = round(m * 100.0);
A 32-bit float
can encode about 232 different values exactly. Due to the binary nature of the typical float, 4.2 is not one of them.
Instead m
has the value of 4.1999998...
// closest float
4.19999980926513671875
// hoped for value
4.2
// next closest float
4.200000286102294921875
Multiplexing by 100 incurs some rounding and the best answer to m*100
is then 419.999969482421875
.
int nm =(m * 100 );
results nm == 419
as assignment of a float
to int
truncates the fractional portion away.
Consider rounding to the nearest integer rather than truncating via int
assignment and use double
constants.
#include <math.h>
// int nm =(m * 100 );
int nm = lround(m * 100.0);
// or
int nm = round(m * 100.0);
edited Nov 19 at 19:36
answered Nov 19 at 19:31
chux
78.9k869145
78.9k869145
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381216%2fwhenever-i-enter-4-2-in-this-code-the-value-of-nm-is-19-where-20-is-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
And that, kids, is why we don't ever rely on transitive equality when pushing a float through an
int
.– WhozCraig
Nov 19 at 19:19
1
It's very tempting just to close this as a duplicate of Is floating point math broken?
– Jonathan Leffler
Nov 19 at 19:57
Possible duplicate of Is floating point math broken?
– Sean Pianka
Nov 19 at 20:50