Sponsored Content
Top Forums UNIX for Dummies Questions & Answers floating point error in linux + C Post 302296808 by headrush on Wednesday 11th of March 2009 11:07:00 PM
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!
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
isgreater(3M)															     isgreater(3M)

NAME
isgreater() - floating-point quiet comparison macro (>) SYNOPSIS
floating-expr floating-expr DESCRIPTION
The macro determines whether its first argument is greater than its second argument. The value of is always equal to however, unlike does not raise the invalid exception when x and y are unordered. The arguments must be of floating type. For HP Integrity servers, the arguments can be any floating type. For PA-RISC, each argument must be either or USAGE
To use the macro, compile either with the default option or with the and options. Make sure your program includes Link in the math library by specifying on the compiler or linker command line. RETURN VALUE
The macro returns the value of The macro raises no floating-point exceptions. ERRORS
No errors are defined. SEE ALSO
isgreaterequal(3M), isless(3M), islessequal(3M), islessgreater(3M), isunordered(3M), math(5). STANDARDS CONFORMANCE
isgreater(3M)
All times are GMT -4. The time now is 04:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy