Archived

This topic is now archived and is closed to further replies.

elis-cool

Can you hold a larger value int or float?

Recommended Posts

elis-cool    271
I know this has probly been asked before but an int only being able to hold around 2 billion is just not good enough for some applications but you can use an unsigned int, then theres a long which is exaclty the same as an int these take up 4 bytes but is there away to make it so an int can hold like 284298473034572457020943503 which is a hell of a lot more than a normal int can hold. I think I may have breifly saw something about increasing the number of bytes a variable uses. Is this advanced? Could some one tell me how it is done or point me to a site?

Share this post


Link to post
Share on other sites
Null and Void    1088
Many compilers have support for "longer than long" integers. The C99 standard calls for a "long long" integer type to do this (which should become 64bits in most 32bit C99 compliant compilers). MSVC has the non-standard __int64 type that you can use. An unsigned 64 bit integer can hold up to 264 .

Floats can hold up to 3.40282*1038 and as low as 1.17549*10-38 on x86 platforms. You shouldn't have a problem with them. But, if you do, use "double" instead of float. Some compilers have a "long double" as well.



Edited by - Null and Void on February 5, 2002 5:06:03 PM

Share this post


Link to post
Share on other sites
jenova    122
float can hold larger numbers. but it will be an approximation.

you might also want to use "__int64".

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
elis-cool    271
I know that floats can only hold 6 decimal places and doubles 15 then after that they get rounded off to zeros after those 6, 15 places have been filled with a proper number, eg a float could hold 2.22222 or something but 2.2222222222222222222 would be rouded off to something like 2.2222200000000000000.
So how many would a __int64 hold and how many would a long double hold?
Is this correct?
    
__int64 var = 5;
long double var2 = 3.14;


Edit: Typo

Edited by - elis-cool on February 5, 2002 5:19:26 PM

Share this post


Link to post
Share on other sites
Beer Hunter    712
__int8, __int16, __int32, __int64 are supported by g++, borland, and msvc, so even if they''re nonstandard, they''re pretty safe to use.

Also, you might want to have a look at this.

Share this post


Link to post
Share on other sites
Null and Void    1088
quote:
Original post by elis-cool
So how many would a __int64 hold and how many would a long double hold?


I already told you that an unsigned 64 bit integer can hold up to 264 (i.e. 1.8446744074*1019). The bit count of a long double changes depending upon the compiler and platform. In x86 GCC it is 96 bits (a normal double is 64 bits). I don''t know the exact limits of that or the precision, but I''m sure you can find out with a little searching.

Share this post


Link to post
Share on other sites
invective    118
quote:

__int8, __int16, __int32, __int64 are supported by g++, borland, and msvc, so even if they''re nonstandard, they''re pretty safe to use



You should wrap any non standard types in a typedef so that if you change platforms or compilers later, it will be easier to update your code.

Share this post


Link to post
Share on other sites
Pyabo    124
What language are you using? Perl has support for arbitrarily large integers.

If __int64 isn''t big enough for your data... I would guess you need to reasses your design, there is something wrong! What are you using this for?

Share this post


Link to post
Share on other sites
Shannon Barber    1681
The x86 native architecture has inherit support for unlimited length integers. You just have to write asm to handle the overflow conditions. 32bit additions will set the carry flag if they overflow (then you can inc the next int in ram). There''s a 32bit multiplication that returns a 64bit value, so you can detect and handle overflow there as well.
(And I think the execution speed would beat the living daylights out of Perl )

Share this post


Link to post
Share on other sites
Kylotan    10007
quote:
Original post by elis-cool
I know that floats can only hold 6 decimal places and doubles 15 then after that they get rounded off to zeros after those 6, 15 places have been filled with a proper number, eg a float could hold 2.22222 or something but 2.2222222222222222222 would be rouded off to something like 2.2222200000000000000.

This isn''t strictly true. Float stands for ''floating point'', which means the decimal point ''floats'' around depending on the requirement of the number. Therefore you are not guaranteed any given number of decimal places or significant digits, as it depends on the size of the number you are trying to represent.




[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
elis-cool    271
Thanks but at the moment I do not know asm, but hopefully I will later in the year... I''m not actualy using it for anything, i''m just curious as an unsigned int just does not seam big enough for some things eg if you were bill gates and wanted to write a program to handle your 100 billion dollar acount.

Also why wont this work?
  
#include <iostream.h>
#include <stdio.h>

int main()
{
__int64 var;
cin >> var;
cout << var;
return 0;
}


The errors generated are:

C:\propertest\hjfjhfd.cpp(7) : error C2679: binary ''>>'' : no operator defined which takes a right-hand operand of type ''__int64'' (or there is no acceptable conversion)

C:\propertest\hjfjhfd.cpp(8) : error C2593: ''operator <<'' is ambiguous

Share this post


Link to post
Share on other sites
Floppy    122
It won't work since iostream doesn't define i/o for 64-bit integers probably since it wasn't a part of the standard. Your'e going to have to do some operator overloading if you want to do that...

(edit: or I guess you could cast it to a different type, but that would be no fun )

Edited by - Floppy on February 6, 2002 1:25:29 AM

Share this post


Link to post
Share on other sites
elis-cool    271
So you reckon some hardcore asm would get me what I want???
I dont want to cast as it will cut short the number and what exactly do you mean by operater overloading in that sense?

Share this post


Link to post
Share on other sites
DrPizza    160
VC++ 7 has overloads for the 64-bit integers. STLPort may do, I''m not sure.

Or you could write your own, it shouldn''t be particularly difficult.

Share this post


Link to post
Share on other sites
elis-cool    271
Also is there higher than __int64? I thought there was a way to explicity state the number of bytes you want the variable to be.

PS. Does anyone know what the difference in the following means, could someone explain it?
    
float num = 1.2;
float num = 1.2f; // ive seen this and want to know typing the f means.



Edit: typo spelt float as flaot.

Edited by - elis-cool on February 6, 2002 2:42:10 AM

Share this post


Link to post
Share on other sites
Neophyte    595
If you don''t specify that you want a float (with the f) most compilers will create a double, and then cast it to a float before assignment.

-Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

Share this post


Link to post
Share on other sites
elis-cool    271
Thanks neophyte but whats the point of saying a float (f) then instead of just leting it be cast into a float from a double it cant be that much slower...

is anyone going to answer this?
quote:

Also is there higher than __int64? I thought there was a way to explicity state the number of bytes you want the variable to be.

Edited by - elis-cool on February 6, 2002 12:21:02 AM

Share this post


Link to post
Share on other sites
Steveyboy    122
Theres not much point in having the ''f'' at all. The compiler will give a warning if the number is too long to fit in a float.
e.g. I think this will cause a warning (if you have the max. warning level set):

float fMonkey = 1.5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555;

Its mostly for completeness - for the same reason that people write:

long lLongNumber = 0x80000000L

(note the ''L'') - its just to emphisise that this is a float or a long.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
There''s a "hyper" type in MFC(though you probably don''t use MFC)
Also you could write a class with a struct in it to hold larger numbers.(although it''ll be hard to operate on those numbers and if you''re developing a 3D Engine then you should probably forget about it.

Share this post


Link to post
Share on other sites