floating point error in linux + C


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers floating point error in linux + C
# 1  
Old 03-12-2009
floating point error in linux + C

Here's a program and its pretty simple .It requires file handling and some calculations but on running it I am not getting the required result.It seems that the code outside the file read's outer while loop is not executing e.g the print statement is not being printed.Plz Help!

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int main(int argc,char *args[])
{
FILE *fp;
char ch;
int load=0,value=0,weight=0;
int i=0,count=0,p,k=1,j,*cost,line=1,u;
if(argc!=2){
printf("\nInsufficient no. of arguments");
exit(0);
}
fp=fopen(args[1],"r");
if(fp==NULL){
printf("\nFile could not be opened");
exit(0);
}
while((ch=fgetc(fp))!='\n'){
if(isdigit(ch)){
p=ch-'0';
load=load*10+p;
}
}
printf("\nload= %d \n",load);
i=0;
while((ch=fgetc(fp)!=EOF)){
value=0;
weight=0;
k=0;
u=0;
while(ch!='\n'){
// printf("\nline=%d,weight=%d,value=%d",line,weight,value);

if((isalpha(ch)||isdigit(ch))&&k==0){
while(ch!=' '){
ch=fgetc(fp);
}
k++;
}
if(weight==0){
while(ch!=' '){
p=ch-'0';
weight=weight*10+p;
ch=fgetc(fp);
}
}
// printf("\nweight=%d",weight);
if(value==0){
while(ch!='\n'){
p=ch-'0';
value=value*10+p;
ch=fgetc(fp);
}
}
// printf("\nValue=%d",value);
if(ch==' '){
while(ch==' '){
ch=fgetc(fp);
}
}
}

u=value/weight;
printf("line=%d weight=%d,value=%d,cost=%d\n",line,weight,value,u);
line++;
}
// count=i-1;
fclose(fp);
return 0;
}

The text file that I am using is strictly of the following format:
1250
LJS93K 1300 10500
J38ZZ9 700 4750
HJ394L 200 3250
01IE82 75 10250

there is a line-break after each line and the first line(1250) is the "load" value,the first column of every is to be avoided the 2nd column is weight and the 3rd column is value.Smilie
One more thing I am using ubuntu distro which uses bash shell!
# 2  
Old 03-12-2009
Code tags for code please, they make the unreadable readable. [ code ] stuff [ /code ] without the extra spaces.

You get 'floating point error' when you divide by zero, check to see if things are getting parsed the way you expect.

You do not need to use fgetc to read in text data! Try scanf. Or better yet, sscanf. Read in data one line at a time then feed it through scanf to get what you want in one go.

Code:
char buf[512], garbage[512];
if(fgets(buf, 512, fp)==NULL0
{
  fprintf(stderr, "Can't read first line\n");
  return(1);
}

if(sscanf(buf, "%d", &load) != 1)
{
  fprintf(stderr, "Can't get load value\n");
  return(1);
}

while(fgets(buf, 512, fp)!=NULL)
{
  if(sscanf(buf, "%s %d %d", garbage, &weight, &value) != 3)
  {
    fprintf(stderr, "Couldn't parse line '%s'\n", buf);
    continue;
  }

  do_stuff();
}

You'll get purists complaining about scanf and its dangers but its far preferable to building your own integer-parsing routines. Smilie Used in this fashion it is fairly safe, with no risk of buffer overflows and none of the strange half-a-line-eaten problems plain scanf is infamous for.

Last edited by Corona688; 03-12-2009 at 12:25 AM.. Reason: adding more code
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

floating point arithmetic operation error

I am writing a script in zsh shell, it fetchs a number from a file using the awk command, store it as a variable, which in my case is a small number 0.62000. I want to change this number by multiplying it by 1000 to become 620.0 using the command in the script var2=$((var1*1000)) trouble is... (2 Replies)
Discussion started by: piynik
2 Replies

2. Programming

Floating Point

Anyone help me i cant found the error of floating point if needed, i added the code complete #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> typedef struct { int hh; int mm; int ss; char nom; int punt; }cancion; typedef struct... (9 Replies)
Discussion started by: Slasho
9 Replies

3. Shell Programming and Scripting

Arithmetic in floating point

is it not possible to simply di aritmetic without using bc or awk i have tried folllowing operatrions but they support only integer types plz suggest me code for floating using values stored in the variables.the ans i get is integer and if i input floating values i get error numeric constant... (6 Replies)
Discussion started by: sumit the cool
6 Replies

4. Shell Programming and Scripting

floating point numbers in if

# if > then > echo "1" > else > echo "2" > fi -bash: How can i compare floating point numbers inside statement? (15 Replies)
Discussion started by: proactiveaditya
15 Replies

5. Shell Programming and Scripting

how to compare 2 floating point no.

Hi, Could any one tell me how to compare to floating point no. using test command. As -eq option works on only intergers. i=5.4 if then echo "equal" else echo "not equal" fi here output will be equal even though no. are unequal. Thanks, ravi (1 Reply)
Discussion started by: useless79
1 Replies

6. Linux

Floating Point Exception

Hi, I am compiling "HelloWorld" C progam on 32-bit CentOS and i want to execute it on 64-bit CentOS architecture. For that i copied the a.out file from 32-bit to 64-bit machine, but while executing a.out file on 64bit machine I am getting "Floating point exception error". But we can run... (3 Replies)
Discussion started by: Mandar123
3 Replies

7. Programming

Floating point error in C

Hi, see the simple code below double i; i=8080.9940; printf(" val :%.30f\n",i); output i m getting is val :8080.993999999999700000000000000 when i m expecting val :8080.9940 what happens?how can i avoid it? thanks... (2 Replies)
Discussion started by: Hara
2 Replies

8. Shell Programming and Scripting

Rounding off the value of Floating point value

Hello, i have some variables say: x=1.4 y=3.7 I wish to round off these values to : x = 2 (after rounding off) y = 4 (after rounding off) I am stuck. Please help. (7 Replies)
Discussion started by: damansingh
7 Replies

9. Shell Programming and Scripting

floating point addition

hi, :) I have a file like this 10.456 123.567 456.876 234.987 ........ ....... What i want to do is ia have to add all those numbers and put the result in some other file. Any help pls. cheers RRK (8 Replies)
Discussion started by: ravi raj kumar
8 Replies

10. Shell Programming and Scripting

Floating Point Division

Does anyone have a simple way of doing floating point ("fp") division? For example, if I divide 3 by 5, I can get 0.6. The built-in calc (`bc`) will perform fp multiplication, but not division, at least not straight-up (i.e., starting bc and just typing in 3/5). I am trying to do this using... (1 Reply)
Discussion started by: gsatch
1 Replies
Login or Register to Ask a Question