473,388 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

about float

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float j = 1.0f, i =j;
int n = 0;
for(; i - j - 1.00f; ++n, j /= 10)
{
;
}
printf("%d", n);

return EXIT_SUCCESS;
}

I supposed the output was 6, because the float's precision is 6, but
as the result, the output is 20.
why?
Mar 21 '08 #1
3 1755
?? said:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float j = 1.0f, i =j;
int n = 0;
for(; i - j - 1.00f; ++n, j /= 10)
{
;
}
printf("%d", n);

return EXIT_SUCCESS;
}

I supposed the output was 6, because the float's precision is 6, but
as the result, the output is 20.
why?
Because i - j - 1.00f will continue to have a non-zero value for that many
iterations (on your system). Imprecision does not imply truncation. If j
is "supposed" to be, say, 0.000100000, it might actually be stored as
something like 0.0001068115234375. And 0.000001 might be stored as
something like 0.0000152587890625. Clearly, with j set to that value, 1.0
- j - 1.0 will be non-zero.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Mar 21 '08 #2
高龙 wrote:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float j = 1.0f, i =j;
int n = 0;
for(; i - j - 1.00f; ++n, j /= 10)
{
;
}
printf("%d", n);

return EXIT_SUCCESS;
}

I supposed the output was 6, because the float's precision is 6, but
as the result, the output is 20.
why?
Try running a version of your program like the following, and examine
your output (which will likely be different from mine). Think about
what it tells you. In particular, think about why j is never, for j < 1,
exactly ten to some negative power.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>

int main(void)
{
float j = 1.0f, i = j;
int hexdigit, decdigit;
int n = 0;
hexdigit = (3 + CHAR_BIT * sizeof j) / 4;
decdigit = (2 + CHAR_BIT * sizeof j) * 0.301;
printf("With this implementation, sizeof(float) = %zu,\n"
"and FLT_DIG = %d. In spite of this, we will\n"
"use precisions of %d (hex) and %d (decimal).\n"
" i = %.*a (%.*g)\n\n",
sizeof(float), FLT_DIG, hexdigit, decdigit,
hexdigit, i, decdigit, i);
for (; i - j - 1.00f; ++n, j /= 10) {
printf("n = %d, j = %.*a (%.*g),\n"
" i - j - 1.f = %.*a (%.*g)\n",
n,
hexdigit, j, decdigit, j,
hexdigit, i - j - 1.f, decdigit, i - j - 1.f);
}
printf("%d\n", n); /* note the end-of-line character at
the end of the last line of output. */

return EXIT_SUCCESS;
}
With this implementation, sizeof(float) = 4,
and FLT_DIG = 6. In spite of this, we will
use precisions of 8 (hex) and 10 (decimal).
i = 0x8.00000000p-3 (1)

n = 0, j = 0x8.00000000p-3 (1),
i - j - 1.f = -0x8.00000000p-3 (-1)
n = 1, j = 0xc.ccccd000p-7 (0.1000000015),
i - j - 1.f = -0xc.cccd0000p-7 (-0.1000000238)
n = 2, j = 0xa.3d70a666p-10 (0.01000000015),
i - j - 1.f = -0xa.3d70a666p-10 (-0.01000000015)
n = 3, j = 0x8.3126e666p-13 (0.0009999999776),
i - j - 1.f = -0x8.3126e666p-13 (-0.0009999999776)
n = 4, j = 0xd.1b716666p-17 (9.999999311e-05),
i - j - 1.f = -0xd.1b716666p-17 (-9.999999311e-05)
n = 5, j = 0xa.7c5ab333p-20 (9.99999902e-06),
i - j - 1.f = -0xa.7c5ab333p-20 (-9.99999902e-06)
n = 6, j = 0x8.637bc000p-23 (9.999998838e-07),
i - j - 1.f = -0x8.637bc000p-23 (-9.999998838e-07)
n = 7, j = 0xd.6bf93333p-27 (9.999998838e-08),
i - j - 1.f = -0xd.6bf93333p-27 (-9.999998838e-08)
8

Mar 21 '08 #3
two Richard(s) help me :)
ADVANCED THANKS!!
Mar 21 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
13
by: agentxx04 | last post by:
Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include<stdio.h> int main() { int item; int a, b, t, mode; int...
6
by: Chad | last post by:
In the following code snippet, I declare the variable val as float in the calc.h header file, and define it as int in test.c. The question is, when I compile this with full warnings, I get no...
3
by: hantechs | last post by:
<html> <body> <p style="width:30%;">text1</p> <p style="float:left;">text2</p> </body> </html> The effect of this html code is : text1 and text2 each is on a line. My question is: Why text2...
4
by: JoeC | last post by:
I am trying to design some complex objects that have quite a bit of data. I understand most syntax but I am trying to learn how to make better design choices. The first question is to OK or good...
1
by: peej | last post by:
I'm using some old code that won't compile any more. I have 2 questions about it, first the code snippet: #define TM_VERTEX_STRIDE 4 vector<float> tm_vertices; appendVertexData(float *...
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
2
by: piuck | last post by:
I download IBM Synthetic Datat for Association rules(from http://www.almaden.ibm.com/cs/projects/iis/hdb/Projects/data_mining/datasets/syndata.html ), After I compiler it, it found a lot of bug......
0
by: Timothy Grant | last post by:
That's because s IS a string. It's not been converted to a float. In : s = '3.1415' In : n = float(s) In : type(s) Out: <type 'str'> In : type(n) Out: <type 'float'> Why are you avoiding...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.