hi everyone,
main()
{
float f=0.7;
if ( f< 0.7)
printf("C");
else
printf("C++");
}
main()
{
float f=0.7;
if(f < 0.7f)
printf("C");
else
printf("C++");
}
The first code snippet prints C and the second one prints C++.Can
anyone tell me the reason for it 11 6428 ra*********@gmail.com opined: hi everyone,
main() { float f=0.7; if ( f< 0.7) printf("C"); else printf("C++"); }
main() { float f=0.7; if(f < 0.7f) printf("C"); else printf("C++"); }
The first code snippet prints C and the second one prints C++.Can anyone tell me the reason for it
No they don't (or at least not necessarily).
a) you do not include <stdio.h> so `printf` is unknown
b) you do not terminate output with '\n'
c) it's `int main(void)`
d) you're missing `return 0;` (or whatever value)
However, your problem is in the fact that undecorated floating point
constants, like `0.7` are of type `double`, by appending 'f', you make
them `float`. Now, if sizes of your `float`s and `double`s are
different representation of 0.7 in them may differ. Adding:
printf("%20.10f %20.10f\n",0.7, 0.7f);
to your code (after fixing the rest) I get (gcc 4.0.3pre, SUSE 10.0):
0.7000000000 0.6999999881
The morale is: if you're going to use floating point numbers in your
applications, carefully study their limitations, and your particular
implementation. Also look into C Standard to see what are the minumum
requirements for a conforming implementation. This advice applies
equally to any programming language you choose.
HTH
--
if (argc > 1 && strcmp(argv[1], "-advice") == 0) {
printf("Don't Panic!\n");
exit(42);
}
(Arnold Robbins in the LJ of February '95, describing RCS)
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
Vladimir S. Oka opined: ra*********@gmail.com opined:
hi everyone,
main() { float f=0.7; if ( f< 0.7) printf("C"); else printf("C++"); }
main() { float f=0.7; if(f < 0.7f) printf("C"); else printf("C++"); }
The first code snippet prints C and the second one prints C++.Can anyone tell me the reason for it
No they don't (or at least not necessarily).
a) you do not include <stdio.h> so `printf` is unknown b) you do not terminate output with '\n' c) it's `int main(void)` d) you're missing `return 0;` (or whatever value)
However, your problem is in the fact that undecorated floating point constants, like `0.7` are of type `double`, by appending 'f', you make them `float`. Now, if sizes of your `float`s and `double`s are different representation of 0.7 in them may differ. Adding:
printf("%20.10f %20.10f\n",0.7, 0.7f);
to your code (after fixing the rest) I get (gcc 4.0.3pre, SUSE 10.0):
0.7000000000 0.6999999881
The morale is: if you're going to use floating point numbers in your applications, carefully study their limitations, and your particular implementation. Also look into C Standard to see what are the minumum requirements for a conforming implementation. This advice applies equally to any programming language you choose.
Oh yes, do not use type `float` it is too small for any serious (and
many less serious) uses. Use `double` instead. And, if I may add, if
you can avoid it, do not use floating point at all.
--
I did this 'cause Linux gives me a woody. It doesn't generate revenue.
(Dave '-ddt->` Taylor, announcing DOOM for Linux)
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
Vladimir S. Oka wrote: Vladimir S. Oka opined:
ra*********@gmail.com opined:
hi everyone,
main() { float f=0.7; if ( f< 0.7) printf("C"); else printf("C++"); }
main() { float f=0.7; if(f < 0.7f) printf("C"); else printf("C++"); }
The first code snippet prints C and the second one prints C++.Can anyone tell me the reason for it No they don't (or at least not necessarily).
a) you do not include <stdio.h> so `printf` is unknown b) you do not terminate output with '\n' c) it's `int main(void)` d) you're missing `return 0;` (or whatever value)
However, your problem is in the fact that undecorated floating point constants, like `0.7` are of type `double`, by appending 'f', you make them `float`. Now, if sizes of your `float`s and `double`s are different representation of 0.7 in them may differ. Adding:
printf("%20.10f %20.10f\n",0.7, 0.7f);
to your code (after fixing the rest) I get (gcc 4.0.3pre, SUSE 10.0):
0.7000000000 0.6999999881
The morale is: if you're going to use floating point numbers in your applications, carefully study their limitations, and your particular implementation. Also look into C Standard to see what are the minumum requirements for a conforming implementation. This advice applies equally to any programming language you choose.
Oh yes, do not use type `float` it is too small for any serious (and many less serious) uses. Use `double` instead. And, if I may add, if you can avoid it, do not use floating point at all.
I recommend both of you read http://docs.sun.com/source/806-3568/ncg_goldberg.html
--
Nils O. Selåsdal www.utelsystems.com
Nils O. Selåsdal wrote: I recommend both of you read http://docs.sun.com/source/806-3568/ncg_goldberg.html
Thanks for the link. I will read it. I may even comment on it (time
permitting).
I'd also like to know why did you feel I should read it as well? Is it
because I did not provide the OP with a long, and detailed, discussion
of finer points of using floating point numbers?
--
BR, Vladimir
thanx for the advice.i didn't include <stdio.h> n left certain
formattings because i thought keeping the code to the min. will help
you people get to the problem immediately.
anyways next time onwards i will post codes which purely complies with
the std.
I know that real constants are "double" in c if not followed by a f but
i saw that question in an aptitude test n that baffled me.
anyways thnx for ur help. ra*********@gmail.com wrote: hi everyone,
main() { float f=0.7; if ( f< 0.7) printf("C"); else printf("C++"); }
main() { float f=0.7; if(f < 0.7f) printf("C"); else printf("C++"); }
The first code snippet prints C and the second one prints C++.Can anyone tell me the reason for it
0.7 cannot be represented exactly in a 32-bit float (assuming a
normalized 23-bit mantissa): the closest you can get without going over
is 0.699981632. It takes 29 bits (again, assuming a normalized
manitssa) to represent 0.7 exactly (provided I've done the math right).
So the first test succeeds, because f (0.699981632) is indeed strictly
less than 0.7. The second test fails, because f (0.699981632) is not
strictly less than 0.7f (0.699981632).
In article <11*********************@j33g2000cwa.googlegroups. com>,
John Bode <jo*******@my-deja.com> wrote: 0.7 cannot be represented exactly in a 32-bit float (assuming a normalized 23-bit mantissa): the closest you can get without going over is 0.699981632. It takes 29 bits (again, assuming a normalized manitssa) to represent 0.7 exactly (provided I've done the math right).
I think you must have done the math wrong.
111 into 1010 goes 1, remainder 11. Quotant so far: 0.1
Shift the remainder, yielding 110
111 into 110 goes 0, remainder 110. Quotant so far: 0.10
Shift the remainder, yielding 1100
111 into 1100 goes 1, remainder 101. Quotant so far: 0.101
Shift the remainder, yielding 1010
This is the same value as we started with, so we have a repeating fraction.
Therefore 0.7 (decimal) is 0.101,101,101... repeated infinitely.
There is no exact and finite base 2 representation of 0.7 (decimal)
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Walter Roberson wrote: In article <11*********************@j33g2000cwa.googlegroups. com>, John Bode <jo*******@my-deja.com> wrote:
0.7 cannot be represented exactly in a 32-bit float (assuming a normalized 23-bit mantissa): the closest you can get without going over is 0.699981632. It takes 29 bits (again, assuming a normalized manitssa) to represent 0.7 exactly (provided I've done the math right).
I think you must have done the math wrong.
111 into 1010 goes 1, remainder 11. Quotant so far: 0.1 Shift the remainder, yielding 110 111 into 110 goes 0, remainder 110. Quotant so far: 0.10 Shift the remainder, yielding 1100 111 into 1100 goes 1, remainder 101. Quotant so far: 0.101 Shift the remainder, yielding 1010 This is the same value as we started with, so we have a repeating fraction.
Therefore 0.7 (decimal) is 0.101,101,101... repeated infinitely. There is no exact and finite base 2 representation of 0.7 (decimal)
Yeah, I mucked it up after 29 terms (serves me right for not
recognizing the pattern). Because my brain is fundamentally damaged,
I chose to do it the hard way and sum powers of 2 until I got close
(which gave me a bit pattern of 0.101001100110011...; the bit pattern
0.101101101... gets me 0.714, btw).
Even so, the point still holds; the value of the float representation
of 0.7 is strictly less than the value of the double representation of
0.7, whereas the value of the float representation of 0.7 is not
strictly less than the value of the float representation of 0.7. ra*********@gmail.com opined: thanx for the advice.i didn't include <stdio.h> n left certain formattings because i thought keeping the code to the min. will help you people get to the problem immediately.
anyways next time onwards i will post codes which purely complies with the std.
I know that real constants are "double" in c if not followed by a f but i saw that question in an aptitude test n that baffled me.
anyways thnx for ur help.
You're welcome, but please also *do* read the link in my sig. Quoting
context is as important, as providing minimal compilable source
(obviously unless you have a problem that it won't compile in the
first place).
--
We are Pentium of Borg. Division is futile. You will be approximated.
(seen in someone's .signature)
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
I know it's a bit out of place, but still c) it's `int main(void)`
it is implementation dependent, int main(void) or int main(int,char**)
are only suggested by the norm.
Kufa wrote: I know it's a bit out of place, but still
c) it's `int main(void)`
it is implementation dependent, int main(void) or int main(int,char**) are only suggested by the norm.
Do not snip attribution lines (the ones saying "Kufa wrote:"). I said
the above.
It is not "only suggested by the norm". The only two forms of `main()`
allowed by the Standard (or, if you want, the only two required to be
supported by all conforming implementations) are:
int main(void)
int main(int argc, char *argv[])
Some prefer the latter as:
int main(int argc, char **argv)
It's a matter of taste, really.
An implementation is allowed to implement, as long as it documents it,
a different sort of `main()`, and that is "implementation dependent".
However, it's not portable.
As this is comp.lang.c (you did find out what its about, didn't you?),
even if DS9K implemented:
double main(float pi)
it would be off-topic, and non-Standard... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jim West |
last post by:
Both
int a;
std::complex<float> b;
std::complex<float> c = static_cast<float>(a)*b;
and
int a;
std::complex<float> b;
|
by: Pepijn Kenter |
last post by:
Dear experts.
I have a vector<float> and want to convert that to a vector<double>. I
optimistically tried:
#include <vector>
#include <iostream>
using namespace std;
int main() {
|
by: bluekite2000 |
last post by:
and why doesnt the standard vector have such conversion available?
|
by: xxx |
last post by:
Hi all, i'm new in visual c++ and i'm having troubles converting types.
Let me explain: i have an unmanaged c++ function that wants an float*
parameter but i have an array<float>^, how i can covert...
|
by: richard_lavoie |
last post by:
Hi,
I have something like this:
vector<floatvec1;
and I want to cast it, so I use
vector vec2<double= static_cast< vector<double(vec1);
I always become a
error: syntax error before `>'...
|
by: Wayne Shu |
last post by:
Hei everyone:
Just see the output of the following program
#include <iostream>
#include <cstdlib>
#include <limits>
int main()
{
std::cout << "minimum exponent of double: " <<
|
by: Ira Gladnick |
last post by:
The following displays as expected in IE7 (div2 floats to the right of
div1). But in Firefox, div2 appears below div1.
Why exactly does this happen in Firefox, and how to make it work as...
|
by: sid |
last post by:
Is it possible to float a <divon top of a <frameset ...?
I need it to overlap parts of the frames.
Thanks
Sid.
|
by: Rares Vernica |
last post by:
Hello,
How does find works for a map where the key is float?
I know you cannot just simply compare floats for equality, you need to
use fabs and some epsilon. I know map does not use equality,...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |