www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - LONG_MAX in D

reply =?ISO-8859-1?Q?Lu=EDs_Marques?= <luismarques+spam gmail.com> writes:
Hi again,

What is the best way to compute LONG_MAX in D? (as of limits.h)
size_t / 2?

Luís
Aug 17 2006
next sibling parent reply nobody <nobody mailinator.com> writes:
Luís Marques wrote:
 Hi again,
 
 What is the best way to compute LONG_MAX in D? (as of limits.h)
 size_t / 2?
 
 Luís
Properties for Integral Types http://digitalmars.com/d/property.html .init initializer (0) .max maximum value .min minimum value long val; long.max // works val.max // also works Also wanted to suggest that D.learn might be a more responsive place to ask this sort of question. This NG seems to be more meta-D than anything-D (while both happen). You will want to come back here to see the next mega wave of discussion about getting import to work, whether consts are good or evil, how auto should work and pretty much anything related to the way classes are implemented.
Aug 17 2006
parent reply =?ISO-8859-1?Q?Lu=EDs_Marques?= <luismarques+spam gmail.com> writes:
nobody wrote:
   long val;
   long.max // works
   val.max  // also works
LONG_MAX is "Maximal value which can be stored in a long int variable" (in C) and differs with platforms (e.g. 32 bit, 64 bit). long.max is fixed for a 64-bit integer.
 Also wanted to suggest that D.learn might be a more responsive place to 
 ask this sort of question. This NG seems to be more meta-D than 
 anything-D (while both happen). You will want to come back here to see 
 the next mega wave of discussion about getting import to work, whether 
 consts are good or evil, how auto should work and pretty much anything 
 related to the way classes are implemented.
Perhaps you are right, but I felt that this isn't a question about learning the language itself and that D.learn might not be the best resource. What are the boundaries? Luís
Aug 17 2006
next sibling parent reply nobody <nobody mailinator.com> writes:
Luís Marques wrote:
 nobody wrote:
   long val;
   long.max // works
   val.max  // also works
LONG_MAX is "Maximal value which can be stored in a long int variable" (in C) and differs with platforms (e.g. 32 bit, 64 bit). long.max is fixed for a 64-bit integer.
Conditional Compilation http://digitalmars.com/d/version.html Predefined Versions X86 Intel and AMD 32 bit processors X86_64 AMD and Intel 64 bit processors I am remembering that when I have used C on my 32 bit machines I am fairly sure a long long was 64 bits and a long int was 32. I went looking and found an old header file that assures me long long is 8 bytes. I can only assume long int is 4 bytes for a 32 bit machine. Assuming you want to define an equivalent using D then it would be done as follows: version(X86) const int LONG_MAX = int.max; version(X86_64) const long LONG_MAX = long.max; Another way to define LONG_MAX conditionally is explained in the Static If Condition of the above page: static if(size_t.sizeof == 4) const int LONG_MAX = int.max; static if(size_t.sizeof == 8) const long LONG_MAX = long.max;
 Also wanted to suggest that D.learn might be a more responsive place 
 to ask this sort of question. This NG seems to be more meta-D than 
 anything-D (while both happen). You will want to come back here to see 
 the next mega wave of discussion about getting import to work, whether 
 consts are good or evil, how auto should work and pretty much anything 
 related to the way classes are implemented.
Perhaps you are right, but I felt that this isn't a question about learning the language itself and that D.learn might not be the best resource. What are the boundaries?
Honestly I have no idea. Most of what I said was completely my own rather short observation (I have not been around very long either). I can say I have never seen anyone complain.
Aug 17 2006
next sibling parent reply =?ISO-8859-1?Q?Lu=EDs_Marques?= <luismarques+spam gmail.com> writes:
nobody wrote:
 Another way to define LONG_MAX conditionally is explained in the  Static 
 If Condition of the above page:
 
   static if(size_t.sizeof == 4)
     const int LONG_MAX = int.max;
 
   static if(size_t.sizeof == 8)
     const long LONG_MAX = long.max;
Well, I don't see much point in that, over "size_t.max / 2" :-) (ah yes, now I notice that I forgot the ".max") Luís
Aug 17 2006
parent nobody <nobody mailinator.com> writes:
Luís Marques wrote:
 nobody wrote:
 Another way to define LONG_MAX conditionally is explained in the  
 Static If Condition of the above page:

   static if(size_t.sizeof == 4)
     const int LONG_MAX = int.max;

   static if(size_t.sizeof == 8)
     const long LONG_MAX = long.max;
Well, I don't see much point in that, over "size_t.max / 2" :-) (ah yes, now I notice that I forgot the ".max") Luís
I can certainly see why you might prefer size_t.max / 2. I would personally use version statements with brackets if I were defining much more (LONG_MIN for example since size_t.min is 0). Anyway you can't say "nobody" warned you! I just pointed out the options. :-)
Aug 17 2006
prev sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
nobody wrote:

 Assuming you want to define an equivalent using D then it would be done 
 as follows:
 
   version(X86)
     const int LONG_MAX = int.max;
 
   version(X86_64)
     const long LONG_MAX = long.max;
Effectively limiting yourself to the Intel archs, of course... See http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Version size_t sounds more useful. --anders
Aug 18 2006
prev sibling next sibling parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
Do you just want to know the maximum value stored in a signed size_t?

Isn't that ptrdiff_t.max?  But if you don't know that, how is it useful? 
(I mean, if you're not using that type to store it I hardly understand 
what use the number is...)

D.learn is for learning the language, D is for talking about the 
language.  As far as I understand.  I'd ask "how do I?" questions in 
D.learn, but that's just me.

-[Unknown]


 nobody wrote:
   long val;
   long.max // works
   val.max  // also works
LONG_MAX is "Maximal value which can be stored in a long int variable" (in C) and differs with platforms (e.g. 32 bit, 64 bit). long.max is fixed for a 64-bit integer.
 Also wanted to suggest that D.learn might be a more responsive place 
 to ask this sort of question. This NG seems to be more meta-D than 
 anything-D (while both happen). You will want to come back here to see 
 the next mega wave of discussion about getting import to work, whether 
 consts are good or evil, how auto should work and pretty much anything 
 related to the way classes are implemented.
Perhaps you are right, but I felt that this isn't a question about learning the language itself and that D.learn might not be the best resource. What are the boundaries? Luís
Aug 17 2006
prev sibling parent reply Don Clugston <dac nospam.com.au> writes:
Luís Marques wrote:
 nobody wrote:
   long val;
   long.max // works
   val.max  // also works
LONG_MAX is "Maximal value which can be stored in a long int variable" (in C) and differs with platforms (e.g. 32 bit, 64 bit).
Why would you want this in a D program? You can probably find what you want in std.stdint.
 long.max is fixed for a 64-bit integer.
 
 Also wanted to suggest that D.learn might be a more responsive place 
 to ask this sort of question. This NG seems to be more meta-D than 
 anything-D (while both happen). You will want to come back here to see 
 the next mega wave of discussion about getting import to work, whether 
 consts are good or evil, how auto should work and pretty much anything 
 related to the way classes are implemented.
Perhaps you are right, but I felt that this isn't a question about learning the language itself and that D.learn might not be the best resource. What are the boundaries?
Many things like this have been posted there in the past. It's not so much a newbies forum as a "how can I do xxx" forum.
Aug 18 2006
parent =?ISO-8859-1?Q?Lu=EDs_Marques?= <luismarques+spam gmail.com> writes:
Don Clugston wrote:
 Why would you want this in a D program?
I'm implementing an API which uses that value. More specifically, I actually need to implement a function which returns LONG_MAX (it's the spec).
Aug 18 2006
prev sibling parent =?ISO-8859-1?Q?Lu=EDs_Marques?= <luismarques+spam gmail.com> writes:
Luís Marques wrote:
 Hi again,
 
 What is the best way to compute LONG_MAX in D? (as of limits.h)
 size_t / 2?
I meant size_t.max / 2, which I'm currently using. Luís
Aug 17 2006