# Is variable a double or long?

Page 1 of 1

## 13 Replies - 529 Views - Last Post: 06 October 2012 - 10:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294510&amp;s=cfe2647d41c42fafad21330bc178ccf7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 The3xorc1st

Reputation: 0
• Posts: 17
• Joined: 05-October 12

# Is variable a double or long?

Posted 06 October 2012 - 08:52 AM

Hi, ok little question i have a variable (double) and have to determine if it can fit in a long (no decimals basically). I tried to do _var1-(long)_var1==0 but of course it only works for relatively small numbers since a long is stored in 4 bits and a double in 8. the problem being i have no idea what to do...is there a way to select only the integer part of a double?
Thank you very much
Is This A Good Question/Topic? 0

## Replies To: Is variable a double or long?

### #2 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Is variable a double or long?

Posted 06 October 2012 - 08:58 AM

I understood only one part of your question

Quote

is there a way to select only the integer part of a double?

Yes, it can be done by type-casting. Something like
```int interger_part = (int)variable;
OR
int interger_part = (long)variable;
```

### #3 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: Is variable a double or long?

Posted 06 October 2012 - 09:13 AM

What language are you using C or C++? What operating system?

What do you consider "relatively small numbers"?

You may want to study this link: Variables, Data Types.

Jim

### #4 The3xorc1st

Reputation: 0
• Posts: 17
• Joined: 05-October 12

## Re: Is variable a double or long?

Posted 06 October 2012 - 09:21 AM

I am using C (sorry forgot that part) and my OS is Windows 7 64bits (home premium).
When i said relatively small i meant that the test works for say, 12 000 but doesnt for 4 000 000 000...

### #5 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: Is variable a double or long?

Posted 06 October 2012 - 09:29 AM

Then you should include <limits.h> and compare your double value to LONG_MAX and insure your double is below that size.

Jim

This post has been edited by jimblumberg: 06 October 2012 - 09:30 AM

### #6 The3xorc1st

Reputation: 0
• Posts: 17
• Joined: 05-October 12

## Re: Is variable a double or long?

Posted 06 October 2012 - 09:37 AM

Strangely enough our teacher is making us work with 16bit processor values...

Hmm i just did your test and ended up with 2 147 483 647
how is that possible???

### #7 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: Is variable a double or long?

Posted 06 October 2012 - 09:39 AM

And I suppose your teacher is using Turbo-C?

What other limitations are there that you haven't mentioned?

What standard does your teacher require/allow you to use? Pre-C89, C89-90, C99, C11?

Hmm i just did your test and ended up with 2 147 483 647
how is that possible???
Because your compiler is not using 16 bit, it's 32 bit.

Jim

This post has been edited by jimblumberg: 06 October 2012 - 09:39 AM

### #8 The3xorc1st

Reputation: 0
• Posts: 17
• Joined: 05-October 12

## Re: Is variable a double or long?

Posted 06 October 2012 - 09:39 AM

Isn't that the value for a 32bit OS?

### #9 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: Is variable a double or long?

Posted 06 October 2012 - 09:45 AM

The maximum's are usually related to the operating system, but more importantly these sizes are compiler specific. What compiler is your instructor using and expecting you to use?

Jim

### #10 The3xorc1st

Reputation: 0
• Posts: 17
• Joined: 05-October 12

## Re: Is variable a double or long?

Posted 06 October 2012 - 09:48 AM

Were using CodeBlocks and the GNU GCC compiler which if i am not mistaken, a 32bit compiler, i guess our teacher didnt think about that...

### #11 Skydiver

• Code herder

Reputation: 4379
• Posts: 14,145
• Joined: 05-May 12

## Re: Is variable a double or long?

Posted 06 October 2012 - 09:56 AM

Ah, since you have a modern compiler, then you tell if a variable is a long, double, or float. Reminiscent of you other thread here's how to tell by letting the compiler tell you what the type is.

```#include <iostream>
#include <typeinfo>

using namespace std;

int main()
{
long x;
double y;
float z;

cout << typeid(x).name() << endl;
cout << typeid(y).name() << endl;
cout << typeid(z).name() << endl;

return 0;
}

```

Anyway, Jim's response in post #5 is the appropriate way to tell if a double value will fit into a long.

### #12 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: Is variable a double or long?

Posted 06 October 2012 - 09:59 AM

Then what version of the compiler (gcc) are you using? If you want to limit yourself to 16 bit values then you may want to look at the short datatype or, if your compiler supports it, the int16_t type from stdint.h.

Jim

This post has been edited by jimblumberg: 06 October 2012 - 10:00 AM

### #13 The3xorc1st

Reputation: 0
• Posts: 17
• Joined: 05-October 12

## Re: Is variable a double or long?

Posted 06 October 2012 - 10:08 AM

Thank you very much for your help, i think i can make it work.
Thank you for taking the time to answer my questions

### #14 jimblumberg

Reputation: 4736
• Posts: 14,769
• Joined: 25-December 09

## Re: Is variable a double or long?

Posted 06 October 2012 - 10:09 AM

Skydiver, on 06 October 2012 - 11:56 AM, said:

Ah, since you have a modern compiler, then you tell if a variable is a long, double, or float. Reminiscent of you other thread here's how to tell by letting the compiler tell you what the type is.

```#include <iostream>
#include <typeinfo>

using namespace std;
.....

```

But remember this only works in C++, the OP stated he is using C, C doesn't have this functionality.

Jim

This post has been edited by jimblumberg: 06 October 2012 - 10:10 AM