473,788 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ 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=12345678901 2.123456;
val1=1234567890 12.123456;
val2=val*val1;
printf("%f\n",v al2);
}

The result I get is 152415787531839 67100000.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 3914

"Yogesh Khanolkar" <yo************ **@azuresolutio ns.com> wrote in message
news:b5******** *************** ***@posting.goo gle.com...
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=12345678901 2.123456;
val1=1234567890 12.123456;
val2=val*val1;
printf("%f\n",v al2);
}

The result I get is 152415787531839 67100000.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************ **@azuresolutio ns.com> wrote:

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


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************ **@azuresolutio ns.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=12345678901 2.123456;
val1=1234567890 12.123456;
val2=val*val1;
printf("%f\n",v al2);
}
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 152415787531839 67100000.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***********@p hysik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oe rring
Nov 13 '05 #4
In <b5************ **************@ posting.google. com> yo************* *@azuresolution s.com (Yogesh Khanolkar) writes:
Hi,

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

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

The result I get is 152415787531839 67100000.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************ **@azuresolutio ns.com> wrote in message
news:b5******** *************** ***@posting.goo gle.com...
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=12345678901 2.123456;
val1=1234567890 12.123456;
val2=val*val1;
printf("%f\n",v al2);
}

The result I get is 152415787531839 67100000.000000 instead of the correct

value.

like: 152415787531839 66898832.546185 383936

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.r etarus.de> wrote in message
news:3F******** *******@develop ment.retarus.de ...
Glen Herrmannsfeldt wrote:

"Yogesh Khanolkar" <yo************ **@azuresolutio ns.com> wrote in message
news:b5******** *************** ***@posting.goo gle.com...

(snip)
val=12345678901 2.123456;
val1=1234567890 12.123456;
val2=val*val1;
printf("%f\n",v al2); The result I get is 152415787531839 67100000.000000 instead
of the correct value.

like: 152415787531839 66898832.546185 383936

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=12345678901 2.123456;
val1=1234567890 12.123456;
val2=val*val1;
ERROR! val2 never declared.
printf("%f\n",v al2);
}

The result I get is 152415787531839 67100000.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.12 3456, f2;
double d1 = 123456789012.12 3456f, d2;
long double ld1 = 123456789012.12 3456L, 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.5241579127474 5e+22
long double: 123456789012.12 3456 squared is 1.5241578753183 9669e+22

--
Martin Ambuhl

Nov 13 '05 #8
Martin Ambuhl <ma*****@earthl ink.net> wrote in message news:<gr******* ***********@new sread2.news.atl .earthlink.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=12345678901 2.123456;
val1=1234567890 12.123456;
val2=val*val1;


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

The result I get is 152415787531839 67100000.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.12 3456, f2;
double d1 = 123456789012.12 3456f, d2;
long double ld1 = 123456789012.12 3456L, 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.5241579127474 5e+22
long double: 123456789012.12 3456 squared is 1.5241578753183 9669e+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************ **@azuresolutio ns.com> wrote:

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


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_Keit h) 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

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

Similar topics

5
3099
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 1111111111111111.02 <---
9
2915
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
1976
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 example of where this would be used (partial code!) double thevar; char abuf; int oc;
69
5603
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 assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after initialisation don't seem to be right. The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
12
43468
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: printf("\n%20s\t%7u\t%13i\t%13i","signed int",sizeof(signed int),INT_MIN,INT_MAX);
34
15996
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
2405
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 enter dice Values:
11
2679
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, excel) so I can analyse the intermediate results. To make sure the data can be understood by other programs, a space need to be included between each cell, and a return at the end of each row (row-major order). Hence at the moment, the dump code...
29
11119
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
9655
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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
10363
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
10110
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
9964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
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
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
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 we have to send another system

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.