473,322 Members | 1,778 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,322 software developers and data experts.

serious doubt

void main()
{

float a=1.9;
if(a<1.9)
cout<<"c";
else
cout<<"c++";
getch();
}
o/p is

c. how is this..

Nov 16 '06 #1
4 1322
prajwalps97 wrote:
void main()
'void main' will make your favorite radio station switch to JackFM in mid
drive-time. Use 'int main'.
{

float a=1.9;
if(a<1.9)
cout<<"c";
else
cout<<"c++";
getch();
}
o/p is

c. how is this..
How is because 1.9 is not precise. A floating-point number is always (deep
breath) a binary representation of a mantissa and an exponent, using a fixed
number of bits for each. There are never enough bits for most fractional
numbers; even 0.1 is frequently very imprecise. Figure out for yourself what
fractional power of 2 is nearest to 0.1 in 64 bits.

Hence never expect two floats to exactly compare. If your app really really
needs a precise 1.9, then multiply your basic unit by 10 and compare 19 as
integers.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Nov 16 '06 #2
Hi

pr*********@gmail.com wrote:
void main()
{

float a=1.9;
if(a<1.9)
cout<<"c";
else
cout<<"c++";
getch();
}
o/p is

c. how is this..
BTW: I think it is generally considered polite to use at least one complete
sentence when interacting with other people. ;-)

Your piece of code does not compile on my computer:

|1: error: ‘::main’ must return ‘int’
| In function ‘int main()’:
|6: error: ‘cout’ was not declared in this scope
|8: error: ‘cout’ was not declared in this scope
|9: error: ‘getch’ was not declared in this scope

Besides fixing those errors, you should have a look at
http://www.parashift.com/c++-faq-lit...html#faq-29.17
It would be a good idea to also read the rest of the FAQ.

hth
Markus

Nov 16 '06 #3
pr*********@gmail.com wrote:
void main()
{

float a=1.9;
if(a<1.9)
cout<<"c";
else
cout<<"c++";
getch();
}
o/p is

c. how is this..
Besides the obvious errors pointed out by Markus, you're
comparing a float and a double. Try

float a = 1.9f;
if (a < 1.9f)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 16 '06 #4
pr*********@gmail.com wrote:
void main()
{

float a=1.9;
if(a<1.9)
cout<<"c";
else
cout<<"c++";
getch();
}
The type of a is float, and it's initialized with a double value 1.9.
The extra bits in the initializer are discarded to convert that double
value to a float. In the comparison, 1.9 is a double, not a float, so
the value of a is promoted to double by appending trailing zeros. The
resulting value is not equal to the double 1.9. If you change the value
in the comparison to 1.9f it will work much better.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 16 '06 #5

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

Similar topics

8
by: Tim | last post by:
Here is a description of my problem... call mysub (5, 10) sub mysub (valA, valB) response.write valB valA = valA - 1 response.write valB end sub
142
by: Herr Lucifer | last post by:
As the founder of .NET framework, Microsoft claims that it invention will be the next best platform for programming in a near future. Now it is 2005, ..NET is 5 years old, and can talk and walk for...
55
by: amanda992004 | last post by:
Excluding the factors of the brain capability, i.e I am not asking about this factor, if you are a single, aside from enjoying coding or debugging, how do you make time to eat properly, i.e...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
2
by: Jensen bredal | last post by:
Hello, I'm quiet confuse after i attended a CMS course. I'm building web apps using asp.net and having looked at the version 2 of asp.net , i doubt wehter it is stil necessary to learn cms. The...
66
by: Cor | last post by:
Hi, I start a new thread about a discussion from yesterday (or for some of us this morning). It was a not so nice discussion about dynamically removing controls from a panel or what ever. It...
122
by: ivan | last post by:
hi all, if I have: if(A && B || C) which operation gets executed first? If I remeber well should be &&, am I correct? thanks
6
by: 182719 | last post by:
Hi, can someone please explain variables and what I am doing wrong here? <script> x=1; alert(x); </script>
1
by: Guy Macon | last post by:
Serious Security Flaw in Google Chrome: http://www.readwriteweb.com/archives/security_flaw_in_google_chrome.php -- Guy Macon <http://www.GuyMacon.com/>
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.