How can I get the min/max possible numeric?
help(numeric)
sends you to help(double)
which has
Double-precision values:
All R platforms are required to work with values conforming tothe
IEC 60559 (also known as IEEE 754) standard. This basically works
with a precision of 53 bits, and represents to that precision a
range of absolute values from about 2e-308 to 2e+308. It also has
special values ‘NaN’ (many of them), plus and minus infinity and
plus and minus zero (although R acts as if these are the same).
There are also _denormal(ized)_ (or _subnormal_) numbers with
absolute values above or below the range given above but
represented to less precision.
See ‘.Machine’ for precise information on these limits. Note that
ultimately how double precision numbers are handled is down to the
CPU/FPU and compiler.
So you want to look at .Machine
which on my 64-bit box has
$double.xmin
[1] 2.22507e-308
$double.xmax
[1] 1.79769e+308
Get min and max integer value containing X number of digits
Fox any x>1, the min value would be 10x-1. E.g.:
- If x = 2, min=102-1 = 101 = 10
- If x = 3, min=103-1 = 102 = 100
- If x = 4, min=104-1 = 103 = 1000
For x=1, since it's the only value that is not a one followed by a series of zeroes, you'll need special treatment.
public static int MinIntWithXDigits(this int x)
{
if (x == 0)
{
throw new ArgumentException(nameof(x), "An integer cannot contain zero digits.");
}
if (x == 1) {
return 0;
}
try
{
return Convert.ToInt32(Math.Pow(10, x - 1));
}
catch
{
throw new InvalidOperationException($"A number with {x} digits cannot be represented as an integer.");
}
}
Side note:
If the result were restricted to positive integers instead of non-negative integers, the smallest integer with one digit would be 1, and the general formula would have applied for this case too.
What is the best way to get the minimum or maximum value from an Array of numbers?
The theoretical answers from everyone else are all neat, but let's be pragmatic. ActionScript provides the tools you need so that you don't even have to write a loop in this case!
First, note that Math.min()
and Math.max()
can take any number of arguments. Also, it's important to understand the apply()
method available to Function
objects. It allows you to pass arguments to the function using an Array
. Let's take advantage of both:
var myArray:Array = [2,3,3,4,2,2,5,6,7,2];
var maxValue:Number = Math.max.apply(null, myArray);
var minValue:Number = Math.min.apply(null, myArray);
Here's the best part: the "loop" is actually run using native code (inside Flash Player), so it's faster than searching for the minimum or maximum value using a pure ActionScript loop.
Maximum and Minimum values for ints
Python 3
In Python 3, this question doesn't apply. The plain int
type is unbound.
However, you might actually be looking for information about the current interpreter's word size, which will be the same as the machine's word size in most cases. That information is still available in Python 3 as sys.maxsize
, which is the maximum value representable by a signed word. Equivalently, it's the size of the largest possible list or in-memory sequence.
Generally, the maximum value representable by an unsigned word will be sys.maxsize * 2 + 1
, and the number of bits in a word will be math.log2(sys.maxsize * 2 + 2)
. See this answer for more information.
Python 2
In Python 2, the maximum value for plain int
values is available as sys.maxint
:
>>> sys.maxint
9223372036854775807
You can calculate the minimum value with -sys.maxint - 1
as shown here.
Python seamlessly switches from plain to long integers once you exceed this value. So most of the time, you won't need to know it.
Explanation on Integer.MAX_VALUE and Integer.MIN_VALUE to find min and max value in an array
but as for this method, I don't understand the purpose of Integer.MAX_VALUE and Integer.MIN_VALUE.
By starting out with smallest
set to Integer.MAX_VALUE
and largest
set to Integer.MIN_VALUE
, they don't have to worry later about the special case where smallest
and largest
don't have a value yet. If the data I'm looking through has a 10
as the first value, then numbers[i]<smallest
will be true (because 10
is <
Integer.MAX_VALUE
) and we'll update smallest
to be 10
. Similarly, numbers[i]>largest
will be true
because 10
is >
Integer.MIN_VALUE
and we'll update largest
. And so on.
Of course, when doing this, you must ensure that you have at least one value in the data you're looking at. Otherwise, you end up with apocryphal numbers in smallest
and largest
.
Note the point Onome Sotu makes in the comments:
...if the first item in the array is larger than the rest, then the largest item will always be Integer.MIN_VALUE because of the else-if statement.
Which is true; here's a simpler example demonstrating the problem (live copy):
public class Example
{
public static void main(String[] args) throws Exception {
int[] values = {5, 1, 2};
int smallest = Integer.MAX_VALUE;
int largest = Integer.MIN_VALUE;
for (int value : values) {
if (value < smallest) {
smallest = value;
} else if (value > largest) {
largest = value;
}
}
System.out.println(smallest + ", " + largest); // 1, 2 -- WRONG
}
}
To fix it, either:
Don't use
else
, orStart with
smallest
andlargest
equal to the first element, and then loop the remaining elements, keeping theelse if
.
Here's an example of that second one (live copy):
public class Example
{
public static void main(String[] args) throws Exception {
int[] values = {5, 1, 2};
int smallest = values[0];
int largest = values[0];
for (int n = 1; n < values.length; ++n) {
int value = values[n];
if (value < smallest) {
smallest = value;
} else if (value > largest) {
largest = value;
}
}
System.out.println(smallest + ", " + largest); // 1, 5
}
}
The maximum value for an int type in Go
https://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli=1
The germane part:
Since integer types use two's complement arithmetic, you can infer the
min/max constant values forint
anduint
. For example,const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1
As per @CarelZA's comment:
uint8 : 0 to 255
uint16 : 0 to 65535
uint32 : 0 to 4294967295
uint64 : 0 to 18446744073709551615
int8 : -128 to 127
int16 : -32768 to 32767
int32 : -2147483648 to 2147483647
int64 : -9223372036854775808 to 9223372036854775807
Get Minimum and maximum numbers from a list
Assuming you can observe any mutation to the list, you could store (cache) the min and max values and possibly update the cached values when a number is added / removed. At this point, the order of the list doens't matter.
Related Topics
Dividing Each Cell in a Data Set by the Column Sum in R
Ggplot and R: Two Variables Over Time
How to Fill Histogram with Color Gradient
Q-Q Plot with Ggplot2::Stat_Qq, Colours, Single Group
Back-To-Back Barplot with Independent Axes R
Finding Close Match from Data Frame 1 in Data Fame 2
Get Data Frame from Character Variable
How to Convert Numeric Values to Time Without the Date
Annotate Ggplot2 Facets with Number of Observations Per Facet
Include Text Control Characters in Plotmath Expressions
Vary the Color Gradient on a Scatter Plot Created with Ggplot2
Why Does Mapply Not Return Date-Objects
Row-Wise Sum of Values Grouped by Columns with Same Name
R Data.Table Fread Command:How to Read Large Files with Irregular Separators
R Predict Function Returning Too Many Values
Splitting String Between Capital and Lowercase Character in R