473,809 Members | 2,699 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

float values can b rounded off?

hi;

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????

Nov 14 '05 #1
13 1337
"sushant" <th********@red iffmail.com> wrote:
int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????


Because you're lying to the compiler. Why should it magically figure out
which of the several possible interpretations of this undefined
behaviour you want?

Richard
Nov 14 '05 #2
sushant wrote:
hi;

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????


1. No proper indentation (stop using Google to post;
get a real NG client).
2. "I MUST NOT FORGET TO INCLUDE STDIO.H" - Homer (well, not really).
3. Ever heard of coercion?

Regards,
Jonathan.

--
"I'm learning to program because then I can write
programs to do my homework faster." - Andy Anfilofieff
Nov 14 '05 #3

sushant wrote:
hi;

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
You're using the wrong conversion specifier in printf(). "%d" expects
an int argument. Use "%f" instead.
return 0;
}
is giving the o/p zero. why??????

Using the wrong conversion specifier leads to unpredictable results.

Nov 14 '05 #4
sushant wrote:
hi;

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????


/* mha: After fixing
* 1) the failure to include <stdio.h>
* 2) the incorrect specifier for a floating-point argument, and
* 3) the failure to end the last line of output with an end-of-line
* character
* we get the working program below
*/
#include <stdio.h>

int main(void)
{
float i = 6.10203;
printf("%g\n", i);
return 0;
}
Nov 14 '05 #5
sushant wrote:

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????


Screw ups:
1. Posting via google and losing all indentation. See sig.
2. Failure to #include <stdio.h>
3. Lying to the compiler about the type of i in the printf.
4. lack of blanks within the code (foolish style).

Either of 2 or 3 result in undefined behavior.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #6

"sushant" <th********@red iffmail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
hi;

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????


The result could be anything or nothing. The behavior
is undefined.

#include <stdio.h>

int main(void)
{
float i = 6.10203;
printf("%f\n", i);
return 0;
}

Tip: Prefer type 'double' over 'float'. It has
a much greater precision and range of values.

-Mike
Nov 14 '05 #7
CBFalconer <cb********@yah oo.com> writes:
sushant wrote:

the code:

int main(void)
{
float i=6.10203;
printf("%d",i);
return 0;
}
is giving the o/p zero. why??????


Screw ups:
1. Posting via google and losing all indentation. See sig.
2. Failure to #include <stdio.h>
3. Lying to the compiler about the type of i in the printf.
4. lack of blanks within the code (foolish style).

Either of 2 or 3 result in undefined behavior.


As well as excessive abbreviation. In the subject, it's "be" not "b".
In the body of the article, "output" is much clearer than "o/p".

I understand there's a common writing style that uses very short
abbreviations like "4" for "for", "2" for "to", and so forth. Please
don't use that style here in comp.lang.c.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
As well as excessive abbreviation. In the subject, it's "be" not "b".
In the body of the article, "output" is much clearer than "o/p".

I understand there's a common writing style that uses very short
abbreviations like "4" for "for", "2" for "to", and so forth. Please
don't use that style here in comp.lang.c.


KT keepin' it real 4 the OG's! You my nigga, dawg!

(Uuuuhhh... that's a good thing, AFAICT)

--
Mabden
Nov 14 '05 #9
Jonathan Burd <jo***********@ REMOVEMEgmail.c om> wrote:
sushant wrote:
float i=6.10203;
printf("%d",i);
1. No proper indentation (stop using Google to post;
get a real NG client).
2. "I MUST NOT FORGET TO INCLUDE STDIO.H" - Homer (well, not really).


Ahem. That's stdio.h, please. C is case-sensitive.
3. Ever heard of coercion?


Yes, but what has it to do with this code?

Richard
Nov 14 '05 #10

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

Similar topics

2
8556
by: Benny | last post by:
Hi, Just wonder why PRINT CAST(0.0573542567654 AS float) will give the rounded reult 0.0573543
5
12620
by: Peter Scheurer | last post by:
Hi, we found some strange behavior when operating with floats and round(). The following simplified statement reproduces the problem. select 6.56 - round(convert(float, 6.56), 2) from sysusers where name = 'public'; =========== -8.88178419
54
8384
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This creates a big hole in a 32-bit timer routine I wrote. Questions. 1. Why does this happen? 2. Is there C macros/functions I can call to tell me when two non-zero numbers are multiplied and the
14
15283
by: ziller | last post by:
Why is it that FLT_DIG (from <float.h>) is 6 while DBL_DIB is 15? Doing the math, the mantissa for floats is 24 bits = 2^24-1 max value = 16,777,215.0f. Anything 8-digit odd # greater than that will be rounded off. For doubles, the mantissa is 53 bits = 2^53-1 max value = 9,007,199,254,740,991.0l (that's an L). So 16 digit odd numbers greater than that will be rounded off. To get the actual precision we take log(base 10) of those...
5
17436
by: Gustaf Liljegren | last post by:
Values altered when I convert from string to float and back again: using System; class FloatTest { static void Main(string args) { string var1 = "800856.22"; Console.WriteLine(var1);
6
7616
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards, Karthi
60
7240
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
11
1704
by: Christian | last post by:
Hi, I have a small problem about distant compilation and float literals. Here is my program : #include <stdio.h> void main() { float f; double d; f = 0.5;
116
36012
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going haywire. So my question is, given this code: int main() { float f = 59.89F;
16
2902
by: Dom Fulton | last post by:
Has anyone got a mechanism for finding the number of bits in floats, doubles, and long doubles? I need this to communicate with some hardware. I guess I could try to deduce this from float.h, but that looks difficult. I don't mind something which is system-specific; the older gccs had various defines in cpu_limits.h and related files, but I can't find anything in later gccs.
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10640
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10387
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.