When I try to store large number into a float like `float varr = 123456789;`

the variable `varr`

has value of `123456792.0`

not `123456789.0`

as I expected. Why?

Is there any solution for this?

I cannot use double or decimal since I'm restricted to 4 bytes in memory.

This is because of the limited precision of a float variable. Use double instead.

As you can see here, float has only a precision of 7 digits, whereas double has 13: http://msdn.microsoft.com/en-us/library/b1e65aza%28v=vs.80%29.aspx

With four bytes of memory you can express approx. 4 Billion different values, i.e. a decimal number with 9 digits. You must ask yourself whether the numbers you want to display fit into that range. E.g. if you have number from 0 to 999 999 999 to store, four bytes are enough. If you have number from 1 000 000 000 to 1 999 999 999 then it is also enough. In this case you need to convert the input number (a double) to an integer by subtracting 1 000 000 000. When reading the value you can perform the inverse.

This principle can be extended to display other ranges of number or larger ranges with less precision. But you have to code the conversion by yourself.

The precision of `float`

type implies this limitation. You can use `double`

type instead.

`float`

type in C# conforms to the IEEE 754-2008 binary32 format (Single-precision floating-point format)

From Wikipedia:

The IEEE 754 standard specifies a binary32 as having:

- Sign bit: 1 bit
- Exponent width: 8 bits
- Significand precision: 24 (23 explicitly stored)
This gives from 6 to 9 significant decimal digits precision (if a decimal string with at most 6 significant decimal is converted to IEEE 754 single precision and then converted back to the same number of significant decimal, then the final string should match the original; and if an IEEE 754 single precision is converted to a decimal string with at least 9 significant decimal and then converted back to single, then the final number must match the original).

Sign bit determines the sign of the number, which is the sign of the significand as well. Exponent is either an 8 bit signed integer from −128 to 127 (2's Complement) or an 8 bit unsigned integer from 0 to 255 which is the accepted biased form in IEEE 754 binary32 definition. For this case an exponent value of 127 represents the actual zero. The true significand includes 23 fraction bits to the right of the binary point and an implicit leading bit (to the left of the binary point) with value 1 unless the exponent is stored with all zeros. Thus only 23 fraction bits of the significand appear in the memory format but the total precision is 24 bits (equivalent to log10(224) ≈ 7.225 decimal digits).

BTW, I don't get your result. I run this code:

```
float varr = 123456789;
Console.WriteLine(varr.ToString("#"));
```

and my output is `123456800`

.

If you can use integers (`int`

), with 32-bit integers (4 bytes), you can store that number, and you won't get any precision problems.

Similar Questions

Possible Duplicate: What should I use for a BigInt class in .NET? I need to represent large numbers in C#. I tried Int64, but now I need to deal with 68! (factorial(68)). I checked UInt64 and it als

How would you generate a very very large random number? I am thinking on the order of 2^10^9 (one billion bits). Any programming language -- I assume the solution would translate to other languages. I

i need a regular expression for decimal/float numbers like 12 12.2 1236.32 123.333 and +12.00 or -12.00 or ...123.123... for using in javascript and jQuery. Thank you.

Based on the IEEE-754 Single Precision standard, how can I know how many normalized numbers can be represented if I know the following: 1 bit for the Sign 8 bits for the exponent 23 bits for the manti

This question already has an answer here: Division result is always zero 3 answers I am doing the following float years = (1/31536000) * 883102.00; and I get years = 0000000 while the actual a

I'm looking at writing a application that need to be able to handle in the region of 200 connections / sec and was wondering if C# and .NET will handle this or if I need to really be looking at C++ to

what the regular expression of a line of string containing ONLY float numbers separated with spaces or tabs. The float number can be negative, like -999.999

I currently have a method to attempt to get a random float between two numbers that I provide however, it does not work properly. For example if I feed in 4.0 as the lower float and 4.5 as the higher

with preg mach i need find all float numbers (x.xx). preg_match_all('/\d+.\d{1,2}/', $test, $list); echo '<pre>'; print_r($list); Now my preg_match find all numbers, but i need only float. How

i have a validation in my .net textbox where it will take only numbers but when i put the the phone format like 080 234234 it will not accept because of a space how to resolve this ? could anyone help

i have a float value that is hundreds of digits long (like the first 100 digits of pi - 3) and need a way to operate on it. is there any way to store and operate on the float that has a large number o

My goal is to split a large, single XML file that contains various content (about 2 to 15 GB) into multiple XML files, each containing a certain entity type, which can later be imported by an SQL data

i am trying to add some float values in python 3 (never tested in 2) and i get some odd results, the only varying factor being the order of the elements in the summation. a = [-1e30, 1e30, 1, 3] print

I have an assignment that requires me to first set up integer arrays to store arbitrarily large numbers. By using each element of an int array, I can hold one digit per element because int variable ty

I've done a good bit of research to find an upload component for .NET that I can use to upload large files, has a progress bar, and can resume the upload of large files. I've come across some componen

I have a large float (primitive) array and not every element in the array is filled. How can i mark a particular element as EMPTY. I understand this can be achieved by some special symbols but still i

Possible Duplicate: Large numbers erroneously rounded in Javascript I am using jQuery.parseJSON() to take a json string and make it an object. The json string is placed in a script tag by our server

Can anyone explain why, in the .NET framework, there are PointF structures (using the single-precision float type) and no PointD (using the double-precision double type)? Did they establish that suc

I'm currently busy with deferred shading in webgl and i need to decode 3 integer value's (in the range [0..256] = 256^3) to a single 32 bit float and encode it later. because this is for WebGL it has

I am using boost library for generating very very large Random numbers in the range [0-2^32-1]. But boost library is not working well with this range. It just displaying an error message Assertion fa

My understanding is that many public key cryptographic algorithms these days depend on large prime numbers to make up the keys, and it is the difficulty in factoring the product of two primes that mak

I have a wcf service that exposes quite a large number of service methods on a single endpoint address. Up to now, all service methods are implemented in a single service contract class. This service

This is a bit of an absurd question that I've had on my mind for a while, but let's say I want to display a very, very, large number, one that cannot be represented by a normal primitive (or combinati

unsigned int deltaTime = now() - last(); unsigned int freq = getFreq(); float timeOfGap = float(deltaTime) / float(freq); ASSERT(0.0f <= timeOfGap, WHAT?); I got a really weired result from my p

Say I have a huge floating number, say a trillion decimal places out. Obviously a long double can't hold this. Let's also assume I have a computer with more than enough memory to hold it. How do you d

Can I work with large numbers (more than 10^400) with built-in method in Delphi?

I have a pretty large formula that has to be calculated about 300 times per second. Most of my variables are float, but some variables are just parameters that go into that formula. In some cases, flo

Using .net To add a $ symbol for numbers I did Texbox1.Text.Format = C How to add % symbol at the end of each integer to represent a string in textbox.

I often use C# to work with large datasets, which take the form of very large lists of strings, or large 2 or 3 dimensional arrays of numbers. The latter especially is very easy to visualize in Matlab

I need to generate a binary file containing only unique random numbers, with single precision. The purpose is then to calculate the entropy of this file and use it with other datasets entropy to calcu

I am looking for a simple method to format the following float\double numbers to a CString. I was hoping to use CString.Format(), but alternatives are welcome as well, as long as it ends up being a CS

In web application, i write the code like this : float f= 659/1024 but i am getting the resule in f is 0.0 where it has to be 0.6458 smething

i have a css page with “float:right; clear:right” for ads and several blocks in the main text that is meant to flow like images, using “float:left”. The problem is that the main text flowbox has a lar

I have floating point numbers in two files. I want to write them in to single file. When I open the file after writing, I cannot see the floating point numbers. I can see some machine code or somethin

I need to compare a float value entered in a web form against a range. The problem is that the client computers may have various locale settings, meaning that user may use either . or , to separat

I'm considering using bittorrent for a large data dissemination problem where the data source is petascale and users will want up to several terabytes. Some details Number of torrents potentially in

Why is it called a single in VB.net? I'm sure there is a good reason but it doesn't seem intuitive to a non formally trained programmer like me.

i have 2 very large binary numbers (144 digits). I want to write them in different RandomAccessFiles and then read the files to memory and check to see which number is bigger. What i did so far: 1. I

What is a good way to bin float precision numbers? something like this, but perhaps more efficient? x = 1; for i = 0,size-1 { // loop over number of bins if (value > x) { ++bin[i]; return; } x *= 0

I have a file with large numbers such as -7.47004e-16 and I am trying to read it into a float array using fscanf(rhs, %f, &numbers[i]); this is in a while loop. But this does not work when we

I noticed that Java VM loads class on demand. So in a large project, if we do a change to a single class and compile then will it affect the currently running java process in the system immediately?

Possible Duplicate: Calculating factorial of large numbers in C Firstly, I am new to C++. I have tried below program to calculate factorial of any number. #include <iostream> using namespace

Does anyone know of an algorithm that fits all of the above criteria? I need to specify a seed number, and a range that I want the output numbers to fall under (which will also be the range that the i

Why does a single-precision floating point number have 7 digit precision (or double 15-16 digits precision)? Can anyone please explain how we arrive on that based on the 32 bits assigned for float(Sig

I have an app that needs to update a large amount of data over a large number of entries. Basically it does some 7,000 inserts and/or updates but it takes a looooong time (like almost 9 minutes... ave

I have the following RegEx ... (\d*,?){1,5}\.?\d{2} ... and it will successfully match everything execpt the single digit 9's in this listing ... 9 99 999 9,999 99,999 999,999 9,999,999 99,999,999 99

I've learned the theory of public key encryption but I'm missing the connection to the physical world. e.g. I've been told that good RSA encryption should rely on prime numbers with 300 decimal digits

I was wondering if you could share best practices and common mistakes when it comes to making large numbers of time-sensitive web service calls. In my case, I have a SOAP and an XML-RPC based web serv

I am puzzled with my code: let sum l = match l with | [] -> 0.0 | h::t -> h +. (sum t);; It should give me the sum of the numbers in the list. However when I check the code, I found the second

I need to port a simple C program to PHP. Currently we have to start the process and parse it's output. The program is very trivial but it is important for the algorithm to use float as the errors wil