Why as.integer() is returning incorrect value?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am using the following expression in my R-script and getting an incorrect value. I was wondering whether it is a bug or I am doing something wrong. Any help will be appreciated.
as.integer(10.7275*1e7)
it is returning 107274999. I was expecting it to return 107275000. How can I fix that?
r
|
show 4 more comments
I am using the following expression in my R-script and getting an incorrect value. I was wondering whether it is a bug or I am doing something wrong. Any help will be appreciated.
as.integer(10.7275*1e7)
it is returning 107274999. I was expecting it to return 107275000. How can I fix that?
r
Are the numbers you are multiplying being generated by a different bit of code? Sometimes it rounds it to fit on the screen, but R holds more accurate information behind the scenes, which could cause this
– RAB
Jan 3 at 4:50
2
print(10.7275, digits = 18)gives> [1] 10.7274999999999991. This is because it is not possible for computers to represent numbers precisely. You might want to useceiling(10.7275*1e7)
– Suren
Jan 3 at 4:55
simply use ceiling(10.7275*1e7)
– Hunaidkhan
Jan 3 at 4:58
Thanks @RAB. I am not using any code other than R. I get same value even if I enter 10.7275 manually (the actual value is a Latitude -10.7275°, i just use the absolute value of it.).
– Nazmul
Jan 3 at 5:02
Many thanks Suren and Hunaidkhan
– Nazmul
Jan 3 at 5:08
|
show 4 more comments
I am using the following expression in my R-script and getting an incorrect value. I was wondering whether it is a bug or I am doing something wrong. Any help will be appreciated.
as.integer(10.7275*1e7)
it is returning 107274999. I was expecting it to return 107275000. How can I fix that?
r
I am using the following expression in my R-script and getting an incorrect value. I was wondering whether it is a bug or I am doing something wrong. Any help will be appreciated.
as.integer(10.7275*1e7)
it is returning 107274999. I was expecting it to return 107275000. How can I fix that?
r
r
edited Jan 3 at 10:58
Wai Ha Lee
6,126124166
6,126124166
asked Jan 3 at 4:46
NazmulNazmul
11
11
Are the numbers you are multiplying being generated by a different bit of code? Sometimes it rounds it to fit on the screen, but R holds more accurate information behind the scenes, which could cause this
– RAB
Jan 3 at 4:50
2
print(10.7275, digits = 18)gives> [1] 10.7274999999999991. This is because it is not possible for computers to represent numbers precisely. You might want to useceiling(10.7275*1e7)
– Suren
Jan 3 at 4:55
simply use ceiling(10.7275*1e7)
– Hunaidkhan
Jan 3 at 4:58
Thanks @RAB. I am not using any code other than R. I get same value even if I enter 10.7275 manually (the actual value is a Latitude -10.7275°, i just use the absolute value of it.).
– Nazmul
Jan 3 at 5:02
Many thanks Suren and Hunaidkhan
– Nazmul
Jan 3 at 5:08
|
show 4 more comments
Are the numbers you are multiplying being generated by a different bit of code? Sometimes it rounds it to fit on the screen, but R holds more accurate information behind the scenes, which could cause this
– RAB
Jan 3 at 4:50
2
print(10.7275, digits = 18)gives> [1] 10.7274999999999991. This is because it is not possible for computers to represent numbers precisely. You might want to useceiling(10.7275*1e7)
– Suren
Jan 3 at 4:55
simply use ceiling(10.7275*1e7)
– Hunaidkhan
Jan 3 at 4:58
Thanks @RAB. I am not using any code other than R. I get same value even if I enter 10.7275 manually (the actual value is a Latitude -10.7275°, i just use the absolute value of it.).
– Nazmul
Jan 3 at 5:02
Many thanks Suren and Hunaidkhan
– Nazmul
Jan 3 at 5:08
Are the numbers you are multiplying being generated by a different bit of code? Sometimes it rounds it to fit on the screen, but R holds more accurate information behind the scenes, which could cause this
– RAB
Jan 3 at 4:50
Are the numbers you are multiplying being generated by a different bit of code? Sometimes it rounds it to fit on the screen, but R holds more accurate information behind the scenes, which could cause this
– RAB
Jan 3 at 4:50
2
2
print(10.7275, digits = 18) gives > [1] 10.7274999999999991. This is because it is not possible for computers to represent numbers precisely. You might want to use ceiling(10.7275*1e7)– Suren
Jan 3 at 4:55
print(10.7275, digits = 18) gives > [1] 10.7274999999999991. This is because it is not possible for computers to represent numbers precisely. You might want to use ceiling(10.7275*1e7)– Suren
Jan 3 at 4:55
simply use ceiling(10.7275*1e7)
– Hunaidkhan
Jan 3 at 4:58
simply use ceiling(10.7275*1e7)
– Hunaidkhan
Jan 3 at 4:58
Thanks @RAB. I am not using any code other than R. I get same value even if I enter 10.7275 manually (the actual value is a Latitude -10.7275°, i just use the absolute value of it.).
– Nazmul
Jan 3 at 5:02
Thanks @RAB. I am not using any code other than R. I get same value even if I enter 10.7275 manually (the actual value is a Latitude -10.7275°, i just use the absolute value of it.).
– Nazmul
Jan 3 at 5:02
Many thanks Suren and Hunaidkhan
– Nazmul
Jan 3 at 5:08
Many thanks Suren and Hunaidkhan
– Nazmul
Jan 3 at 5:08
|
show 4 more comments
2 Answers
2
active
oldest
votes
This is due to computer inaccuracy with decimals (as mentioned in comments by @Suren). You can get around it by:
as.integer(ceiling(10.7275*1e7))
Note:
ceiling(as.integer(10.7275*1e7))
will not work (it will return the value you are getting now).
add a comment |
Its a floating point issue. One quick fix would be to use double precision floating point for better accuracy:
as.double(10.7275*1e7) # alternatively as.numeric()
# [1] 107275000
Also refer to this floating point post to get more information and resolutions for floating point math as commented by @Wai Ha Lee
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f54016488%2fwhy-as-integer-is-returning-incorrect-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is due to computer inaccuracy with decimals (as mentioned in comments by @Suren). You can get around it by:
as.integer(ceiling(10.7275*1e7))
Note:
ceiling(as.integer(10.7275*1e7))
will not work (it will return the value you are getting now).
add a comment |
This is due to computer inaccuracy with decimals (as mentioned in comments by @Suren). You can get around it by:
as.integer(ceiling(10.7275*1e7))
Note:
ceiling(as.integer(10.7275*1e7))
will not work (it will return the value you are getting now).
add a comment |
This is due to computer inaccuracy with decimals (as mentioned in comments by @Suren). You can get around it by:
as.integer(ceiling(10.7275*1e7))
Note:
ceiling(as.integer(10.7275*1e7))
will not work (it will return the value you are getting now).
This is due to computer inaccuracy with decimals (as mentioned in comments by @Suren). You can get around it by:
as.integer(ceiling(10.7275*1e7))
Note:
ceiling(as.integer(10.7275*1e7))
will not work (it will return the value you are getting now).
answered Jan 3 at 5:08
RABRAB
1,416418
1,416418
add a comment |
add a comment |
Its a floating point issue. One quick fix would be to use double precision floating point for better accuracy:
as.double(10.7275*1e7) # alternatively as.numeric()
# [1] 107275000
Also refer to this floating point post to get more information and resolutions for floating point math as commented by @Wai Ha Lee
add a comment |
Its a floating point issue. One quick fix would be to use double precision floating point for better accuracy:
as.double(10.7275*1e7) # alternatively as.numeric()
# [1] 107275000
Also refer to this floating point post to get more information and resolutions for floating point math as commented by @Wai Ha Lee
add a comment |
Its a floating point issue. One quick fix would be to use double precision floating point for better accuracy:
as.double(10.7275*1e7) # alternatively as.numeric()
# [1] 107275000
Also refer to this floating point post to get more information and resolutions for floating point math as commented by @Wai Ha Lee
Its a floating point issue. One quick fix would be to use double precision floating point for better accuracy:
as.double(10.7275*1e7) # alternatively as.numeric()
# [1] 107275000
Also refer to this floating point post to get more information and resolutions for floating point math as commented by @Wai Ha Lee
edited Jan 6 at 15:35
answered Jan 3 at 4:58
Mankind_008Mankind_008
1,5682413
1,5682413
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.
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%2f54016488%2fwhy-as-integer-is-returning-incorrect-value%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

Are the numbers you are multiplying being generated by a different bit of code? Sometimes it rounds it to fit on the screen, but R holds more accurate information behind the scenes, which could cause this
– RAB
Jan 3 at 4:50
2
print(10.7275, digits = 18)gives> [1] 10.7274999999999991. This is because it is not possible for computers to represent numbers precisely. You might want to useceiling(10.7275*1e7)– Suren
Jan 3 at 4:55
simply use ceiling(10.7275*1e7)
– Hunaidkhan
Jan 3 at 4:58
Thanks @RAB. I am not using any code other than R. I get same value even if I enter 10.7275 manually (the actual value is a Latitude -10.7275°, i just use the absolute value of it.).
– Nazmul
Jan 3 at 5:02
Many thanks Suren and Hunaidkhan
– Nazmul
Jan 3 at 5:08