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

1

8

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?

Eradicate answered 5/4, 2019 at 23:39 Comment(5)
@Camilo - #26316518 is very different question (this is likely duplicate - #11520243 seem to be good choice explaining why, also will need to add integres there)Erskine
@AlexeiLevenkov How is it a different question if it's the same compilation error?Weakness
@CamiloTerevinto OP is looking to get float and not to handle large integers. Since you can't really mix float/double with BigInteger there is not much value to constructing constant as BigInteger to use in float/double computations down the road.Erskine
@AlexeiLevenkov Except the OP is declaring a BigInteger rather than a float. I would agree if I at least saw a .1 there. There's nothing in the post that says the OP wants a non-integral data-typeWeakness
@CamiloTerevinto I don't see how float myValue = … can be considered as attempt to use BigInteger... but that's my interpretation of the post. At any rate with two duplicates and an answer OP should have enough information to resolve their problem.Erskine
R
9

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;
Rodman answered 5/4, 2019 at 23:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.