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

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
isgreaterequal(3M)														isgreaterequal(3M)

NAME
isgreaterequal() - floating-point quiet comparison macro (>=) SYNOPSIS
floating-expr floating-expr DESCRIPTION
The macro determines whether its first argument is greater than or equal to 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
isgreater(3M), isless(3M), islessequal(3M), islessgreater(3M), isunordered(3M), math(5). STANDARDS CONFORMANCE
isgreaterequal(3M)
All times are GMT -4. The time now is 07:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy