Jump to content

INT vs Double?

- - - - -

  • This topic is locked This topic is locked
9 replies to this topic

#1
Steamsteam

Steamsteam

    Newbie

  • Members
  • PipPip
  • 27 posts
I am most likely wrong but I just did a test using both INT and Double. Double was capable of holding both whole and not whole numbers whereas INT was only capable of holding whole numbers. To my understanding Double also holds much larger numbers as well as smaller numbers. Why would I choose to use INT where I can use double? Even if I know the number will be whole like asking for x number of months in a year. Why not just use double?

Thanks for the speedy reply!

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
The sizes of data types differ in different platforms, but a double should store the mantissa and exponent in 8 bytes or 64 bits which ranges +/- 1.7e +/- 308 to roughly ~15 digits signed. This is double that of a normal 4 byte or 32 bit integer which ranges -2,147,483,648 to 2,147,483,647 signed.

You should use them if you are wanting to work with decimal numbers that are double precision, or rather slightly more precise than floats, otherwise you should use an integer or float. It is sluggish to the processor opcodes otherwise, but a good compiler with optimization should change it to the appropriate data type anyway.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
double is a floating-point type whereas int is an integer type. A floating point type is capable of holding non-integer numbers. double is just float with 64 bits rather than 32. A long double is the same thing but with 80 bits.
Programming is a journey, not a destination.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

DarkLordofthePenguins said:

double is a floating-point type whereas int is an integer type. A floating point type is capable of holding non-integer numbers. double is just float with 80 bits rather than 32.
64 bits (1 for sign, 11 bits for the exponent and 52 bits for the mantissa) not 80. 80 would mess with the natural stack alignment.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts

Nullw0rm said:

64 bits (1 for sign, 11 bits for the exponent and 52 bits for the mantissa) not 80. 80 would mess with the natural stack alignment.

Yes, I realized my mistake after I made that post and corrected it. I was thinking of long doubles, which are 80 bits. It's been a while since I did any C.
Programming is a journey, not a destination.

#6
Steamsteam

Steamsteam

    Newbie

  • Members
  • PipPip
  • 27 posts
Thanks guys! I +REPd you dark, but am unable to rep you null. I think its just lag :) I will try again in a bit ;)

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Something important to be aware of: int's can be used in case statements, doubles can't. Also, it is common that you will assign a double a value like 10.0, and it will fail when compared with 10, because a double's IEEE representation can't always exactly represent an integer value.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Floats also can't be transmitted across a network in any portable format (except as literal strings, I think). Ints can, however.
sudo rm -rf /

#9
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Following gives yet another e.g. of where doubles might be insufficient where integers are

http://forum.codecal...html#post305122
Today is the first day of the rest of my life

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Please don't respond to threads more than a month old. The people involved have moved on (I had no memory of this when I got the notification) and it pushes newer, more active threads down in the list.

Closed.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users