What is the Max value for 'float'? [duplicate]

When I check the value of "float.MaxValue" I'm getting:

3.402823E+38

which is:

340,282,300,000,000,000,000,000,000,000,000,000,000

Then why when I'm trying to set a much smaller value into a float variable:

float myValue = 1234567890123456789024;

then I get an error message:

"Integral constant is too large" ?

This value is MUCH smaller then "3.402823E+38", so why am I getting an error for it?

5

1 Answer

Most Numeric types have a MaxValue Field

Single.MaxValue Field

Represents the largest possible value of Single. This field is constant.

Which equates to

public const float MaxValue = 3.402823E+38; 

However in this case, you need to put use f suffix to specify a type of a numerical literal, otherwise it will interpret it as an integral type (on a cascading scale of max range up to uint64).

float myValue = 1234567890123456789024f; 

Additional Resources

Value types table (C# Reference)

Compiler Error CS1021

Integral constant is too large

A value represented by an integer literal is greater than UInt64.MaxValue.

UInt64.MaxValue Field

Represents the largest possible value of UInt64. This field is constant.

public const ulong MaxValue = 18446744073709551615; 

You Might Also Like