473,490 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

incorrect printf o/p for doubles

Hi,

I am getting incorrect o/p from the code below:

#include<stdio.h>
#include<float.h>
#include<limits.h>
main()
{
double val,val1;
val=123456789012.123456;
val1=123456789012.123456;
val2=val*val1;
printf("%f\n",val2);
}

The result I get is 15241578753183967100000.000000 instead of the correct value.

I am running on HP-UX 11 with ansi c compiler.

Can anyone let me know why I am getting this result.
Nov 13 '05 #1
13 3875

"Yogesh Khanolkar" <yo**************@azuresolutions.com> wrote in message
news:b5**************************@posting.google.c om...
Hi,

I am getting incorrect o/p from the code below:

#include<stdio.h>
#include<float.h>
#include<limits.h>
main()
{
double val,val1;
val=123456789012.123456;
val1=123456789012.123456;
val2=val*val1;
printf("%f\n",val2);
}

The result I get is 15241578753183967100000.000000 instead of the correct value.
I am running on HP-UX 11 with ansi c compiler.

Can anyone let me know why I am getting this result.


It looks close enough for me. What answer did you expect?

-- glen
Nov 13 '05 #2
On 2003-10-30, Yogesh Khanolkar <yo**************@azuresolutions.com> wrote:

#include<stdio.h>
#include<float.h>
#include<limits.h>
main()
{
double val,val1;
val=123456789012.123456;
val1=123456789012.123456;
val2=val*val1;
printf("%f\n",val2);
}


It looks like val2 is defaulting to an int type, and you are trying to
print it out like a double.

-- James
Nov 13 '05 #3
Yogesh Khanolkar <yo**************@azuresolutions.com> wrote:
I am getting incorrect o/p from the code below: #include<stdio.h>
#include<float.h>
#include<limits.h>
main()
{
double val,val1;
val=123456789012.123456;
val1=123456789012.123456;
val2=val*val1;
printf("%f\n",val2);
}
This won't compile because 'val2' is never declared. Additional
problems are the missing 'int' in front of main() (you only get
away with this when you don't use a C99 compiler) and the
missing return statement at the end - main() always returns an int.
The result I get is 15241578753183967100000.000000 instead of the correct value.


That seems to the best approximation your machine is able to come up
with - floating point variables have only a finite precision. And 18
digits doesn't look too bad. Welcome to the wonderful world of
floating point math ;-) And since you're already including <float.h>
check how many digits the compiler is promising you by printing out
the value of DBL_DIG.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #4
In <b5**************************@posting.google.com > yo**************@azuresolutions.com (Yogesh Khanolkar) writes:
Hi,

I am getting incorrect o/p from the code below:

#include<stdio.h>
#include<float.h>
What for?
#include<limits.h>
What for?
main()
{
double val,val1;
val=123456789012.123456;
val1=123456789012.123456;
val2=val*val1; ^^^^
Huh?!? I can't believe that your compiler didn't complain!
printf("%f\n",val2);
}

The result I get is 15241578753183967100000.000000 instead of the correct value.


HUGE clue: what is the "correct" value and why? Additional clue: make
sure to base your answer on the value of DBL_DIG as defined by your
implementation in <float.h>.

And next time, please post the actual code you have compiled.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5
Glen Herrmannsfeldt wrote:

"Yogesh Khanolkar" <yo**************@azuresolutions.com> wrote in message
news:b5**************************@posting.google.c om...
Hi,

I am getting incorrect o/p from the code below:

#include<stdio.h>
#include<float.h>
#include<limits.h>
main()
{
double val,val1;
val=123456789012.123456;
val1=123456789012.123456;
val2=val*val1;
printf("%f\n",val2);
}

The result I get is 15241578753183967100000.000000 instead of the correct

value.

like: 15241578753183966898832.546185383936

maybe float/double is not the best bet, doing scientific/financial calculations.
There are libraries and other languages, which do this natively.
Look them up after you've learned C (others have told you the main-thing and
more).

Wolfgang

I am running on HP-UX 11 with ansi c compiler.

Can anyone let me know why I am getting this result.


It looks close enough for me. What answer did you expect?

-- glen

Nov 13 '05 #6

"Wolfgang Riedel" <wo*************@development.retarus.de> wrote in message
news:3F***************@development.retarus.de...
Glen Herrmannsfeldt wrote:

"Yogesh Khanolkar" <yo**************@azuresolutions.com> wrote in message
news:b5**************************@posting.google.c om...

(snip)
val=123456789012.123456;
val1=123456789012.123456;
val2=val*val1;
printf("%f\n",val2); The result I get is 15241578753183967100000.000000 instead
of the correct value.

like: 15241578753183966898832.546185383936

maybe float/double is not the best bet, doing scientific/financial

calculations. There are libraries and other languages, which do this natively.
Look them up after you've learned C (others have told you the main-thing and more).


Floating point was designed for, and works well for, scientific quantities
that have inherent uncertainty.

Possibly the most important constant for most people, Newton's G, is known
only to about four decimal digits. (That is what holds us down onto the
earth's surface.) For numbers with such relative uncertainty (relative to
the size of the number), especially when they can be very big or very small,
floating point is convenient and useful.

For numbers with absolute uncertainty (that doesn't depend on the size of
the number), such as financial or typesetting calculations, fixed point
values where the unit corresponds to the measuring unit in use, tend to give
better results.

The product above has 35 decimal digits, or about 112 bits of precision.
There are machines with floating point types that long, or close to that
long. If they are supported by C, they tend to be called long double.
(Some CDC machines came close with double, but I don't know of any are still
running.)

Otherwise, the usual implementation of float has six or seven digits of
precision, and double about 16.

-- glen
Nov 13 '05 #7
Yogesh Khanolkar wrote:
Hi,

I am getting incorrect o/p from the code below:

#include<stdio.h>
#include<float.h>
#include<limits.h>
main()
{
double val,val1;
val=123456789012.123456;
val1=123456789012.123456;
val2=val*val1;
ERROR! val2 never declared.
printf("%f\n",val2);
}

The result I get is 15241578753183967100000.000000 instead of the correct value.


The real mysteries are
(1) what your real code is, since yours doesn't compile, and
(2) what you might have been expecting.
Run the code below and see what your implementation gives you.
FLT_DIG may be as small as 6, and DBL_DIG and LDBL_DIG as small as 10.
Your assignment statements assume DBL_DIG to be 18 and the "right" answer
requires DBL_DIG to be at least 35. I doubt the you machine really has
doubles of more than 40 octets.

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

int main(void)
{
float f1 = 123456789012.123456, f2;
double d1 = 123456789012.123456f, d2;
long double ld1 = 123456789012.123456L, ld2;
f2 = f1 * f1;
printf("float: %.*g squared is %.*g\n", FLT_DIG, f1, FLT_DIG, f2);
d2 = d1 * d1;
printf("double: %.*g squared is %.*g\n", DBL_DIG, d1, DBL_DIG, d2);
ld2 = ld1 * ld1;
printf("long double: %.*Lg squared is %.*Lg\n", LDBL_DIG, ld1,
LDBL_DIG, ld2);
return 0;
}
float: 1.23457e+11 squared is 1.52416e+22
double: 123456790528 squared is 1.52415791274745e+22
long double: 123456789012.123456 squared is 1.52415787531839669e+22

--
Martin Ambuhl

Nov 13 '05 #8
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<gr******************@newsread2.news.atl.eart hlink.net>...
Yogesh Khanolkar wrote:
Hi,

I am getting incorrect o/p from the code below:

#include<stdio.h>
#include<float.h>
#include<limits.h>
main()
{
double val,val1;
val=123456789012.123456;
val1=123456789012.123456;
val2=val*val1;


ERROR! val2 never declared.
printf("%f\n",val2);
}

The result I get is 15241578753183967100000.000000 instead of the correct value.


The real mysteries are
(1) what your real code is, since yours doesn't compile, and
(2) what you might have been expecting.
Run the code below and see what your implementation gives you.
FLT_DIG may be as small as 6, and DBL_DIG and LDBL_DIG as small as 10.
Your assignment statements assume DBL_DIG to be 18 and the "right" answer
requires DBL_DIG to be at least 35. I doubt the you machine really has
doubles of more than 40 octets.

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

int main(void)
{
float f1 = 123456789012.123456, f2;
double d1 = 123456789012.123456f, d2;
long double ld1 = 123456789012.123456L, ld2;
f2 = f1 * f1;
printf("float: %.*g squared is %.*g\n", FLT_DIG, f1, FLT_DIG, f2);
d2 = d1 * d1;
printf("double: %.*g squared is %.*g\n", DBL_DIG, d1, DBL_DIG, d2);
ld2 = ld1 * ld1;
printf("long double: %.*Lg squared is %.*Lg\n", LDBL_DIG, ld1,
LDBL_DIG, ld2);
return 0;
}
float: 1.23457e+11 squared is 1.52416e+22
double: 123456790528 squared is 1.52415791274745e+22
long double: 123456789012.123456 squared is 1.52415787531839669e+22


I am extermely sorry for posting incorrect code. I forgot to paste the
val2 declaration of double.
Nov 13 '05 #9
James Hu <jx*@despammed.com> writes:
On 2003-10-30, Yogesh Khanolkar <yo**************@azuresolutions.com> wrote:

#include<stdio.h>
#include<float.h>
#include<limits.h>
main()
{
double val,val1;
val=123456789012.123456;
val1=123456789012.123456;
val2=val*val1;
printf("%f\n",val2);
}


It looks like val2 is defaulting to an int type, and you are trying to
print it out like a double.


Undeclared variables don't default to int. Some compilers, especially
old ones, might let you get away with

val2;

as a declaration for val2 with a default type of int, but it's never
been possible to omit the declaration altogether.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #10
Keith Thompson <ks*@cts.com> wrote in message news:<lz************@cts.com>...
James Hu <jx*@despammed.com> writes:
On 2003-10-30, Yogesh Khanolkar <yo**************@azuresolutions.com> wrote:

{
double val,val1;
val=123456789012.123456;
val1=123456789012.123456;
val2=val*val1;


It looks like val2 is defaulting to an int type, and you are trying to
print it out like a double.


Undeclared variables don't default to int. Some compilers, especially
old ones, might let you get away with

val2;

as a declaration for val2 with a default type of int, but it's never
been possible to omit the declaration altogether.


Crikes! Thanks for the correction.

-- James
Nov 13 '05 #11
On Fri, 31 Oct 2003, Keith Thompson wrote:
Undeclared variables don't default to int. Some compilers, especially
old ones, might let you get away with

val2;

as a declaration for val2 with a default type of int, but it's never
been possible to omit the declaration altogether.


Never in standard C at least, but where does this odd interpretation
come from then? Did K&R allow it or is it just some awfully widespread
hack?

Nov 13 '05 #12
On 2003-11-01, Jarno A Wuolijoki <jw******@cs.Helsinki.FI> wrote:
On Fri, 31 Oct 2003, Keith Thompson wrote:
Undeclared variables don't default to int. Some compilers, especially
old ones, might let you get away with

val2;

as a declaration for val2 with a default type of int, but it's never
been possible to omit the declaration altogether.


Never in standard C at least, but where does this odd interpretation
come from then? Did K&R allow it or is it just some awfully widespread
hack?


Many pre-ANSI C compilers supported a syntax along the lines of:

var;

to declare a global integer variable. It also supported a syntax of

foo(var)
var; /* this line is optional */
{
/* ... */
}

to declare an integer parameter. (As an aside, gcc still accepts
the above syntax.)

I incorrectly assumed pre-ANSI C compilers allowed the implicit
declaration of an integer variable, similar to the way pre-ANSI C
and C90 do for a function returning int.

-- James
Nov 13 '05 #13
In <lz************@cts.com> Keith Thompson <ks*@cts.com> writes:
James Hu <jx*@despammed.com> writes:
On 2003-10-30, Yogesh Khanolkar <yo**************@azuresolutions.com> wrote:
>
> #include<stdio.h>
> #include<float.h>
> #include<limits.h>
> main()
> {
> double val,val1;
> val=123456789012.123456;
> val1=123456789012.123456;
> val2=val*val1;
> printf("%f\n",val2);
> }
>


It looks like val2 is defaulting to an int type, and you are trying to
print it out like a double.


Undeclared variables don't default to int. Some compilers, especially
old ones, might let you get away with

val2;

as a declaration for val2 with a default type of int, but it's never
been possible to omit the declaration altogether.


And that is/was allowed at file scope only, because at block scope this
looks like an expression statement with an undeclared identifier.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #14

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

Similar topics

5
3082
by: dustu | last post by:
Hi, I'm using lcc-win32 on Windows 2000 as compiler/IDE. The details are: Input file (sample - the no. of digits per line may change): ---> 1111111111111111.00 1111111111111111.01...
9
2890
by: | last post by:
void show( char *s, ...) is a function seemd like prinf code -------------- #include <stdio.h> #include <stdarg.h> void show( char *s, ...) { va_list stage;
2
1955
by: David Mathog | last post by:
Is there some method for restricting the maximum number of characters that will be emitted by a printf() function? Where printf() here is generic and refers to sprintf, fprintf, etc. Here's an...
69
5488
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
12
43393
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: ...
34
15827
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
1
2368
by: ndawg123 | last post by:
Hey guys what im trying to do is write a yatzee game with C. And im stuck already and its the start?!?! I want the user to type there 5 numbers. i.e My program so far does this Please...
11
2642
by: vectorizor | last post by:
Hi guys, My program deals with with lots of large matrices. To easily debug it, I would like to be able to dump them in files, so that I can easily import then in other software (think matlab,...
29
11048
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
0
7108
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
6967
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
7181
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...
1
6847
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...
0
4565
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...
0
3078
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...
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
272
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...

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.