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?

phuclv
  • 37,963
  • 15
  • 156
  • 475
John White
  • 101
  • 1
  • 1
  • 2
  • @Camilo - https://stackoverflow.com/questions/26316018/integral-constant-is-too-large-cs1021-how-to-put-1000-extremely-big-70-dig is very different question (this is likely duplicate - https://stackoverflow.com/questions/11519743/why-is-the-f-required-when-declaring-floats seem to be good choice explaining why, also will need to add integres there) – Alexei Levenkov Apr 06 '19 at 00:02
  • @AlexeiLevenkov How is it a different question if it's the same compilation error? – Camilo Terevinto Apr 06 '19 at 00:03
  • @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. – Alexei Levenkov Apr 06 '19 at 00:09
  • @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-type – Camilo Terevinto Apr 06 '19 at 00:11
  • @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. – Alexei Levenkov Apr 06 '19 at 00:15

1 Answers1

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;
TheGeneral
  • 79,002
  • 9
  • 103
  • 141