Unix Programming - Problem with scanf on solaris 10

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2006 > Problem with scanf on solaris 10





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Problem with scanf on solaris 10
ramanchalotra@gmail.com

2006-02-15, 7:57 am

Deat All

I have written a piece of code as


#include<stdio.h>


main()
{
float f;
printf("\n Enter a float value ");
scanf("%f",&f);
printf("\n Value = %f\n ",f);




}



when i ran this code the output looks as



Enter a float value 1234.678
Value = 1234.677979


Where i am making mistake ..

Please help

Regards,
Raman

Pascal Bourguignon

2006-02-15, 7:57 am

ramanchalotra@gmail.com writes:

> Deat All
> I have written a piece of code as
> #include<stdio.h> main()
> {
> float f;
> printf("\n Enter a float value ");
> scanf("%f",&f);
> printf("\n Value = %f\n ",f);
> }
> when i ran this code the output looks as
> Enter a float value 1234.678
> Value = 1234.677979


Which is perfectly correct.


> Where i am making mistake ..


The mistake is in not reading:
What Every Computer Scientist Should Know About Floating-Point Arithmetic
http://portal.acm.org/citation.cfm?id=103163




If you expect a number with 7 significant decimal digits (ie. a C
float), you should print only 7 significant decimal digits:

--> printf("\n Value = %.6e\n ",f);

[pjb@thalassa tmp]$ ./f

Enter a float value 1234.678

Value = 1.234678e+03


--
__Pascal Bourguignon__ http://www.informatimago.com/

"You question the worthiness of my code? I should kill you where you
stand!"
Rainer Temme

2006-02-15, 7:57 am

ramanchalotra@gmail.com wrote:

> float f;
> printf("\n Enter a float value ");
> scanf("%f",&f);
> printf("\n Value = %f\n ",f);


> Enter a float value 1234.678
> Value = 1234.677979


> Where i am making mistake ..


Hi Raman,

The only mistake I can see, is that you expect the float
value be "exactly" like you entered it.

Due to the nature of floating-point representation there
is no "exact" representation for this number. What is
stored is the nearest float-number to the one you wanted.



.... Rainer
Andrei Voropaev

2006-02-15, 7:57 am

On 2006-02-15, ramanchalotra@gmail.com <ramanchalotra@gmail.com> wrote:
> I have written a piece of code as

[...]
> printf("\n Enter a float value ");
> scanf("%f",&f);
> printf("\n Value = %f\n ",f);

[...]
> when i ran this code the output looks as
> Enter a float value 1234.678
> Value = 1234.677979
>
> Where i am making mistake ..


Nowhere. Everything is correct. Computer can't handle floats absolutely
precise like integers. Read http://en.wikipedia.org/wiki/Floating_point



--
Minds, like parachutes, function best when open
Giorgos Keramidas

2006-02-15, 7:57 am

On 15 Feb 2006 04:00:56 -0800, ramanchalotra@gmail.com wrote:
> Deat All
>
> I have written a piece of code as
>
> #include<stdio.h>
>
> main()
> {
> float f;
> printf("\n Enter a float value ");
> scanf("%f",&f);
> printf("\n Value = %f\n ",f);
> }
>
> when i ran this code the output looks as
>
> Enter a float value 1234.678
> Value = 1234.677979
>
> Where i am making mistake ..


There are various `problems' with this program:

- The main function is incorrect. Sun Studio 10 reports:

"foo.c", line 5: warning: old-style declaration or incorrect type for: main

A similar message is displayed by GCC:

foo.c:5: warning: return type defaults to `int'

- You are not returning any status at all from main(), even though its
lack of a proper function declaration means it defaults to 'returning
an int'.

- Output to stdout may be line-buffered. This means that your prompt,
which doesn't end with a newline may never appear until a newline has
reached standard output. Your program will then happily block,
waiting for input in scanf(), but the user won't know anything about
the reason the program appears to do nothing at all. Not good.

You should definitely use something like:

printf("Enter a float value: ");
fflush(stdout);

- It's probably bad style to print a newline and then a space, as in:

printf("\n Foo");

This ends up showing as a very awkward space at the first column of my
terminal:

|keramida@beatrix:/home/keramida$ ./a.out
|
| Enter a float value: 123
|
| Value = 123.000000
|keramida@beatrix:/home/keramida$

Similarly, printing a newline right after scanf() has returned from a
line-buffered input call, results in a 'double newline', shown above
as an empty line between the two lines displayed by your printf()
calls.

- Most importantly, you are using 'float', which has limited precision.
It's not strange that you get a limited precision. Try this instead:

1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int
5 main(void)
6 {
7 double f;
8
9 printf("Enter a float value: ");
10 fflush(stdout);
11
12 scanf("%lf", &f);
13
14 printf("Value = %lf\n", f);
15 return EXIT_SUCCESS;
16 }

This should work fine:

keramida@beatrix:/home/keramida$ cc -Xc foo.c -lm
keramida@beatrix:/home/keramida$ ./a.out
Enter a float value: 1234.678
Value = 1234.678000
keramida@beatrix:/home/keramida$

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com