473,401 Members | 2,125 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,401 software developers and data experts.

Comparisons

#include<stdio.h>

int main()
{
int a=1;
float b=1.0;
float c=1.1;
double d=1.1;
long double e=1.1;

if(a==b) //true
printf("\ntrue");
else
printf("\nfalse");
if(c==d) //false
printf("\ntrue");
else
printf("\nfalse");

if(c==e) //false
printf("\ntrue");
else
printf("\nfalse");

if(d==e) //true
printf("\ntrue");
else
printf("\nfalse");

return 0;

}

Output:
true
false
false
true

Please justify the outputs.

Feb 10 '06 #1
8 1613

Akhil wrote:
#include<stdio.h>

int main()
{
int a=1;
float b=1.0;
float c=1.1;
double d=1.1;
long double e=1.1;

if(a==b) //true
printf("\ntrue");
else
printf("\nfalse");
if(c==d) //false
printf("\ntrue");
else
printf("\nfalse");

if(c==e) //false
printf("\ntrue");
else
printf("\nfalse");

if(d==e) //true
printf("\ntrue");
else
printf("\nfalse");

return 0;

}

Output:
true
false
false
true

Please justify the outputs.

I got this as a homework question during my college days ......
Hint : look for more about "float" on the web ......

Feb 10 '06 #2
Akhil said:
Please justify the outputs.


http://docs.sun.com/source/806-3568/ncg_goldberg.html

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Feb 10 '06 #3
Akhil wrote:
Output:
true
false
false
true Please justify the outputs.


Certainly.

true
false
false
true

There we are: right 'and justified.

--
Chris "try, or try not -- there is no do" Dollin
Feb 10 '06 #4
In article <11**********************@z14g2000cwz.googlegroups .com>,
"Akhil" <ak*************@gmail.com> wrote:
#include<stdio.h>

int main()
{
int a=1;
float b=1.0;
float c=1.1;
double d=1.1;
long double e=1.1;

if(a==b) //true
printf("\ntrue");
else
printf("\nfalse");
if(c==d) //false
printf("\ntrue");
else
printf("\nfalse");

if(c==e) //false
printf("\ntrue");
else
printf("\nfalse");

if(d==e) //true
printf("\ntrue");
else
printf("\nfalse");

return 0;

}

Output:
true
false
false
true

Please justify the outputs.


Describe as precisely as you can which values are stored in b, c, d and
e.
Feb 10 '06 #5
On 9 Feb 2006 21:27:32 -0800, "Akhil" <ak*************@gmail.com>
wrote:
#include<stdio.h>

int main()
{
int a=1;
float b=1.0;
float c=1.1;
double d=1.1;
long double e=1.1;

if(a==b) //true
printf("\ntrue");
else
printf("\nfalse");
if(c==d) //false


This is a FAQ. See:
http://c-faq.com/fp/strangefp.html

Also worth a read is "Computer Representation of Numbers":
http://www.eskimo.com/~scs/cclass/progintro/sx5.html

Nick.

Feb 10 '06 #6
Chris Dollin wrote:
Akhil wrote:
Output:
true
false
false
true

Please justify the outputs.


Certainly.
true
false
false
true

There we are: right 'and justified.


Dagnabit. I was about to write a similar smart alec response,
mentioning format strings, and pointing out that the output was
already left justified. But you beat me to it.

--
"If you want to post a followup via groups.google.com, 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
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Feb 10 '06 #7
"Akhil" <ak*************@gmail.com> writes:
#include<stdio.h>

int main()
{
int a=1;
float b=1.0;
float c=1.1;
double d=1.1;
long double e=1.1;

if(a==b) //true
printf("\ntrue");
else
printf("\nfalse");
if(c==d) //false
printf("\ntrue");
else
printf("\nfalse");

if(c==e) //false
printf("\ntrue");
else
printf("\nfalse");

if(d==e) //true
printf("\ntrue");
else
printf("\nfalse");

return 0;

}


Aside from your actual question, I'd like to make some more points
about your code.

You have a "\n" at the beginning of each string you print. It should
be at the end. If there's no newline at the end of your program's output,
it might not even appear, depending on the system.

Proper indentation makes code easier to read. So does judicious use
of whitespace.

"int main()" is acceptable, but "int main(void)" is preferred.

"//" comments are valid in C99, and are supported by many C compilers,
but they're not supported by the C89/C90 standard. They're also
discouraged in Usenet postings, since they can cause problems with
line wrapping.

Applying all these suggestions, your code becomes:

#include <stdio.h>

int main(void)
{
int a = 1;
float b = 1.0;
float c = 1.1;
double d = 1.1;
long double e = 1.1;

if (a == b) /* true */
printf("true\n");
else
printf("false\n");

if (c == d) /* false */
printf("true\n");
else
printf("false\n");

if (c == e) /* false */
printf("true\n");
else
printf("false\n");

if (d == e) /* true */
printf("true\n");
else
printf("false\n");

return 0;
}

(I also use braces ({ ... }) even for single statements in control
structures.)

--
Keith Thompson (The_Other_Keith) 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.
Feb 10 '06 #8
On 9 Feb 2006 21:27:32 -0800, in comp.lang.c , "Akhil"
<ak*************@gmail.com> wrote:

(some code comparing floats)

http://docs.sun.com/source/806-3568/ncg_goldberg.html

also secton 14 of the FAQ
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 11 '06 #9

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

Similar topics

7
by: A.M. Kuchling | last post by:
python.org has a page of "Python vs. X" language comparisons at <http://www.python.org/doc/Comparisons.html>. They're all pretty outdated, and often unfair because they're written by a person who...
0
by: Mike Rovner | last post by:
Mike Rovner wrote: > A.M. Kuchling wrote: >> python.org has a page of "Python vs. X" language comparisons at >> <http://www.python.org/doc/Comparisons.html>. They're all pretty >> outdated, and...
11
by: Dietrich Epp | last post by:
Without invoking double-underscore black magic, it is possible to choose a, b, and c in Python such that: a < b b < c c < a This could cause sorting functions to malfunction. >>> class...
5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
3
by: Kevin King | last post by:
I have a question about an assignment I have. I need to count the number of comparisons in my merge sort. I know that the function is roughly nlog(n), but I am definately coming up with too many...
10
by: Peter Ammon | last post by:
I'm fuzzy on exactly what I can depend on when doing floating point comparisons. I know that calculated values can differ, e.g. I'm not guaranteed that 1. + 2. == 3. What about comparisons with...
4
by: Aamir Mahmood | last post by:
Hi I am on framework version 1.1 (SP1). Consider the following code. public static void Main() { int i=1; int j=1;
7
by: Jim Michaels | last post by:
This is a simple coding tip I learned to prevent comparisons from becoming assignments (and this becoming a hair-pulling session during debugging): in comparisons, try to put your constants on...
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
2
by: Nishad | last post by:
Is anybody know how to write xpath for string Value Comparisons thank you
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.