What is the largest integer number that can be stored in a float in C#?

Shervan360 1,541 Reputation points
2024-09-07T19:38:51.4166667+00:00

Hello,

According to Microsoft Docs, we can store an integer up to 3.4 * 10^38, so why can't I store 100,000,001 in a float?

Well, 100,000,001 is less than 3.4 * 10^38.

Screenshot 2024-09-07 230742

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,858 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 64,566 Reputation points
    2024-09-07T20:54:22.7866667+00:00

    the issue is floating is significant digits X base to a power. in school you might have learned scientific notation:

    3.345 * 10^9 = 3345000000

    you may remember doing really large numbers (high exponent) but only worrying about a low significant digits (probably under 5).

    when stored in a computer there are fixed number of digits. some are allocated to the decimal with an implied decimal point and some to the exponent. if we allocated 10 digits we can say have 8 significant digits and 2 digits for exponents (we would need an extra bit for a negative indicator). so with system 100,000,001 wild round to 1.0 x 10^8.

    in computers instead of radix (base) 10, we use radix 2. and unlike scientific notation, where we use one whole number, in computer there is no whole. so 1 = .1 x 10^1

    a small int (16 bits) can hold a value between -32768 and 32767. you get a full 4 digits of precision, but can store some 5 digit precision digits.

    with a float we use a total of 32 bits. one bit is used to indicate positive or negative number. 23 bits are the decimal number, and 8 bits (which includes a sign bit) are the exponent.

    full details:

    https://en.wikipedia.org/wiki/IEEE_754

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.