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

floating point problem in C

Following is the small code snippet which is giving strange results .

--------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6.    float f= 1256.35;
  7.    char ch[4];
  8.  
  9.    printf("\n1. f  : %f",f);
  10.    memset(ch,'\0',strlen(ch) );
  11.    printf("\n2. f  : %f",f);
  12.    return 0;
  13. }
  14.  
--------------------------------------------------------------------------------

Expected output is
1. f : 1256.349976
2. f : 1256.349976

But I am getting the output
1. f : 1256.349976
2. f : 0.000000

Can you help in getting the solution of this problem. I have tried it on sun-solaris as well as on VC++ platform and I am getting the same results.
Sep 27 '07 #1
3 1166
Ok Friends,
I got the solution of this problem. Actually in place of strlen() in memset() call ;
we need to use the sizeof().
So the modified code which is now working correctly
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5.    float f= 1256.35;
  6.    char ch[4];
  7.  
  8.    printf("\n1. f  : %f",f);
  9.    memset(ch,'\0',sizeof(ch) );
  10.    printf("\n2. f  : %f",f);
  11.    return 0;
  12. }
  13.  
  14.  
Following is the small code snippet which is giving strange results .

--------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6.    float f= 1256.35;
  7.    char ch[4];
  8.  
  9.    printf("\n1. f  : %f",f);
  10.    memset(ch,'\0',strlen(ch) );
  11.    printf("\n2. f  : %f",f);
  12.    return 0;
  13. }
  14.  
--------------------------------------------------------------------------------

Expected output is
1. f : 1256.349976
2. f : 1256.349976

But I am getting the output
1. f : 1256.349976
2. f : 0.000000

Can you help in getting the solution of this problem. I have tried it on sun-solaris as well as on VC++ platform and I am getting the same results.
Sep 27 '07 #2
ashitpro
542 Expert 512MB
Ok Friends,
I got the solution of this problem. Actually in place of strlen() in memset() call ;
we need to use the sizeof().
So the modified code which is now working correctly
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5.    float f= 1256.35;
  6.    char ch[4];
  7.  
  8.    printf("\n1. f  : %f",f);
  9.    memset(ch,'\0',sizeof(ch) );
  10.    printf("\n2. f  : %f",f);
  11.    return 0;
  12. }
  13.  
  14.  

strlen looks for '\0', that we don't have in 'ch',
So strlen will return unspecified value(length till it gets the '\0').
In my case it used to return 17 instead of 4.
And very next statement(memset) makes next 17 bytes zeroed and probably overwriting the float value.
Sep 27 '07 #3
Thanks yar,

that is the thing i m looking for.

Regards
Raj Kr Arora

strlen looks for '\0', that we don't have in 'ch',
So strlen will return unspecified value(length till it gets the '\0').
In my case it used to return 17 instead of 4.
And very next statement(memset) makes next 17 bytes zeroed and probably overwriting the float value.
Sep 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

31
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
24
by: j0mbolar | last post by:
C supports single precision floating point and double precision floating point but does it support fixed floating point? i've read that fixed floating point is more accurate than single precision...
7
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.