开发者

How to Convert double to int in C?

double a;
a = 3669.0;
int b;
b = a;

I am getting 3668 in b, instead of 3669.

How do I fix This problem? And if have 3559.8 like that also I want li开发者_运维知识库ke 3559 not 3560.


I suspect you don't actually have that problem - I suspect you've really got:

double a = callSomeFunction();
// Examine a in the debugger or via logging, and decide it's 3669.0

// Now cast
int b = (int) a;
// Now a is 3668

What makes me say that is that although it's true that many decimal values cannot be stored exactly in float or double, that doesn't hold for integers of this kind of magnitude. They can very easily be exactly represented in binary floating point form. (Very large integers can't always be exactly represented, but we're not dealing with a very large integer here.)

I strongly suspect that your double value is actually slightly less than 3669.0, but it's being displayed to you as 3669.0 by whatever diagnostic device you're using. The conversion to an integer value just performs truncation, not rounding - hence the issue.

Assuming your double type is an IEEE-754 64-bit type, the largest value which is less than 3669.0 is exactly

3668.99999999999954525264911353588104248046875

So if you're using any diagnostic approach where that value would be shown as 3669.0, then it's quite possible (probable, I'd say) that this is what's happening.


main() {
    double a;
    a=3669.0;
    int b;
    b=a;
    printf("b is %d",b);
  
}

output is :b is 3669

when you write b=a; then its automatically converted in int

see on-line compiler result : 

http://ideone.com/60T5b


This is called Implicit Type Conversion Read more here https://www.geeksforgeeks.org/implicit-type-conversion-in-c-with-examples/


This is the notorious floating point rounding issue. Just add a very small number, to correct the issue.

double a;
a=3669.0;
int b;
b=a+ 1e-9;


How to Convert double to int in C?

Quick answer. (Does not meet OP's implied goal need of rounding.):

// Discard the fractional part of d - truncation.
// Well defined when the truncated d is near the int range.
int i = (int) d;

Conversions from double to int should address some issues.

  • What to do when the the double value's fractional portion is near 1.0, as in OP's true case? Often a rounded value is preferred.

  • How to detect/handle an double well out of the int range?

  • What about infinity and non-a-number?

If range detection not important, round and convert in one step with long lround(double): "... round their argument to the nearest integer value, rounding halfway cases away from zero, regardless of the current rounding direction".

#include <math.h>
int = (int) lround(d); // Since C99.

If range detection is important, 1st use double math to detect range. Take care to get the edge case test correct as conversion to int valid from [INT_MIN - 0.9999... to INT_MAX + 0.9999...]. Be sure the math/values used are exact as needed. (double) INT_MAX may lose precision when int have more value bits than double has precision.

#include <limits.h>
#define INT_MAX_PLUS1  ((INT_MAX/2 + 1)*2.0)
#define INT_MIN_MINUS1 ((INT_MIN/2 - 1)*2.0)

#if INT_MIN == -INT_MAX
// Rare non-2's complement
if ((d < INT_MAX_PLUS1) && (d > INT_MIN_MINUS1)) {
#else
// 2's complement
if ((d < INT_MAX_PLUS1) && (d + INT_MIN > -1.0)) {
#endif

  i = (int) lround(d);

} else {
  fprintf(stderr, "Value %.17g not in int range [%d %d]\n", INT_MIN, INT_MAX);
}

Commonly d < INT_MAX_PLUS1 is false when d is a not-a-number. C does not require this behavior, even though false is common. Alternatively:

#include <math.h>
if (!isnan(d) && (d < INT_MAX_PLUS1) && (d > INT_MIN_MINUS1)) {
...


int b;
double a;
a=3669.0;
b=a;
printf("b=%d",b);

this code gives the output as b=3669 only you check it clearly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜