13 Replies - 359 Views - Last Post: 06 October 2012 - 10:09 AM Rate Topic: -----

#1 The3xorc1st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • 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;

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,300
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 The3xorc1st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,300
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 The3xorc1st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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???
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,300
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 The3xorc1st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,300
  • 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
Was This Post Helpful? 0
  • +
  • -

#10 The3xorc1st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,300
  • 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

Was This Post Helpful? 0
  • +
  • -

#13 The3xorc1st  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :rockon:
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: Is variable a double or long?

Posted 06 October 2012 - 10:09 AM

View PostSkydiver, 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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1