Sponsored Content
Top Forums Programming Testing floating point numbers Post 302493881 by solaris_user on Friday 4th of February 2011 04:34:43 AM
Old 02-04-2011
Testing floating point numbers

Hi guys

I have problem with my simple calculator, author of my book wrote

Quote:
Store all values as floats. Whenever an is reguired , determine whether the fractional part of the number is zero
One way I tried is to test if one the inpur number is grater than zero, and then substatct

And my protptype function is
Code:
#include <stdio.h> 

int main(void) {
    
    float a, b , result;
    
    printf("\t\tThis program is only a test for %% C function\n");
    printf("Enrer two numbers: ");
    scanf("%f %f", &a, &b);
    
    // Test if input number has fractional part or not 
    
    if ( (a > 0) && (a - (int) a ) == 0 && (b > 0) && (b - (int) b ) == 0) {
        (int) (result = ((int) a) % ((int)b));
        printf("Result: %d\n", result);
        return 0;
      }
    else if ( (a < 0) && a + ( (int) - (a) ) == 0 && (b < 0) && b + ( (int)-(b) ) == 0) {
        (int) (result = ((int) a) % ((int)b));
        printf("Result: %d\n", result);
        return 0;
      }
    else if ( (a > 0) && (a - (int) a ) == 0 && (b < 0) && b - ( (int) - (b) ) == 0) {
        (int) (result = ((int) a) % ((int)b));
        printf("Result: %d\n", result);
        return 0;
      }
    else if ((a < 0) && a - ((int)-(a)) == 0  && (b > 0) && (b - (int) b ) == 0) {
        (int) (result = ((int) a) % ((int)b));
        printf("Result: %d\n", result);
        return 0;
        }
    else
        printf("Cannot perform action\n");
        return 1;
    }

But this is too complicated for basic calculator. I don't my C code look like spaghetti.

My simple algorithm (flowchart) is what I have on my mind, but litle black box (diamond) is that I don't know how to implement in

http://img62.imageshack.us/img62/4655/diagram1o.jpg

Last edited by solaris_user; 02-04-2011 at 05:40 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

problem with floating point numbers in awk

hi all, i have the following problem using awk in a script i want to read the values from a column with real numbers and calculate the mean.the problem is that when i use a statement such as this num = $4 i cant find a way to convert the variable from string to floating point to perform... (7 Replies)
Discussion started by: kanagias
7 Replies

2. Shell Programming and Scripting

How to Compare Floating point / real numbers

Hai, Can you please guide me, to compare the floating point numbers. Eg. If then echo "value1 is grater " fi This code is not working properly when i excuted with floating values or real numbers (13 Replies)
Discussion started by: padarthy
13 Replies

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

4. Shell Programming and Scripting

sed to extract only floating point numbers from HTML

Hi All, I'm trying to extract some floating point numbers from within some HTML code like this: <TR><TD class='awrc'>Parse CPU to Parse Elapsd %:</TD><TD ALIGN='right' class='awrc'> 64.50</TD><TD class='awrc'>% Non-Parse CPU:</TD><TD ALIGN='right' class='awrc'> ... (2 Replies)
Discussion started by: pondlife
2 Replies

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

6. Shell Programming and Scripting

How to compare floating point numbers in shell script?

How can we compare 2 floating point numbers in SHELL script? (11 Replies)
Discussion started by: dearanik
11 Replies

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

8. UNIX for Dummies Questions & Answers

Add floating point numbers from file

How do I use bash to add all the floating point numbers saved in a file like this? 490.47 244.61 263.07 131.59 246.81 115.20 (3 Replies)
Discussion started by: locoroco
3 Replies

9. Shell Programming and Scripting

Floating Point Numbers in c shell!

I have started using bash but this script which I am working on it, is in c chell. So here is my simple problem: set x = 0.4124\0.234 echo $x 0.4124.0.234 Same operation in Bash gives me correct result in my terminal. So there is something with my c shell that is causing this behaviour.... (8 Replies)
Discussion started by: dixits
8 Replies

10. Shell Programming and Scripting

Comparison of floating point numbers in bash

I have the following code snippet in bash if ]; then minm=`echo "$diff" | bc` fi It works well for most of the cases. However lets say diff is -0.17 and minm is -0.0017. In such a case the comparison seems to fail. Is the correct way to compare a mixture of positive and... (12 Replies)
Discussion started by: ngabrani
12 Replies
Scalar::Number(3pm)					User Contributed Perl Documentation				       Scalar::Number(3pm)

NAME
Scalar::Number - numeric aspects of scalars SYNOPSIS
use Scalar::Number qw(scalar_num_part); $num = scalar_num_part($scalar); use Scalar::Number qw(sclnum_is_natint sclnum_is_float); if(sclnum_is_natint($value)) { ... if(sclnum_is_float($value)) { ... use Scalar::Number qw(sclnum_val_cmp sclnum_id_cmp); @sorted_nums = sort { sclnum_val_cmp($a, $b) } @floats; @sorted_nums = sort { sclnum_id_cmp($a, $b) } @floats; DESCRIPTION
This module is about the numeric part of plain (string) Perl scalars. A scalar has a numeric value, which may be expressed in either the native integer type or the native floating point type. Many values are expressible both ways, in which case the exact representation is insignificant. To fully understand Perl arithmetic it is necessary to know about both of these representations, and the differing behaviours of numbers according to which way they are expressible. This module provides functions to extract the numeric part of a scalar, classify a number by expressibility, and compare numbers across representations. This module is implemented in XS, with a pure Perl backup version for systems that can't handle XS. FUNCTIONS
Each "sclnum_" function takes one or more scalar numeric arguments to operate on. These arguments must be numeric; giving non-numeric arguments will cause mayhem. See "is_number" in Params::Classify for a way to check for numericness. Only the numeric value of the scalar is used; the string value is completely ignored, so dualvars are not a problem. Decomposition scalar_num_part(SCALAR) Extracts the numeric value of SCALAR, and returns it as a pure numeric scalar. The argument is permitted to be any scalar. Every scalar has both a string value and a numeric value. In pure string scalars, those resulting from string literals or string operations, the numeric value is determined from the string value. In pure numeric scalars, those resulting from numeric literals or numeric operations, the string value is determined from the numeric value. In the general case, however, a plain scalar's string and numeric values may be set independently, which is known as a dualvar. Non-plain scalars, principally references, determine their string and numeric values in other ways, and in particular a reference to a blessed object can stringify and numerify however the class wishes. This function does not warn if given an ostensibly non-numeric argument, because the whole point of it is to extract the numeric value of scalars that are not pure numeric. Classification sclnum_is_natint(VALUE) Returns a truth value indicating whether the provided VALUE can be represented in the native integer data type. If the floating point type includes signed zeroes then they do not qualify; the only zero representable in the integer type is unsigned. sclnum_is_float(VALUE) Returns a truth value indicating whether the provided VALUE can be represented in the native floating point data type. If the floating point type includes signed zeroes then an unsigned zero (from the native integer type) does not qualify. Comparison sclnum_val_cmp(A, B) Numerically compares the values A and B. Integer and floating point values are compared correctly with each other, even if there is no available format in which both values can be accurately represented. Returns -1, 0, +1, or undef, indicating whether A is less than, equal to, greater than, or not comparable with B. The "not comparable" situation arises if either value is a floating point NaN (not- a-number). All flavours of zero compare equal. This is very similar to Perl's built-in <=> operator. The only difference is the capability to compare integer against floating point (where neither can be represented exactly in the other's format). <=> performs such comparisons in floating point, losing accuracy of the integer value. sclnum_id_cmp(A, B) This is a comparison function supplying a total ordering of scalar numeric values. Returns -1, 0, or +1, indicating whether A is to be sorted before, the same as, or after B. The ordering is of the identities of numeric values, not their numerical values. If floating point zeroes are signed, then the three types (positive, negative, and unsigned) are considered to be distinct. NaNs compare equal to each other, but different from all numeric values. The exact ordering provided is mostly numerical order: NaNs come first, followed by negative infinity, then negative finite values, then negative zero, then unsigned zero, then positive zero, then positive finite values, then positive infinity. In addition to sorting, this function can be useful to check for a zero of a particular sign. BUGS
In Perl 5.6, if configured with a wider-than-usual native integer type such that there are native integers that can't be represented exactly in the native floating point type, it is not always possible to distinguish between integer and floating point values in pure Perl code. In order to get the full benefit of either type, one is expected (by the numeric semantics) to know in advance which of them one is using. The pure Perl version of this module can't operate on such a system, but the XS version works fine. This problem is resolved by Perl 5.8's new numeric semantics. SEE ALSO
Data::Float, Data::Integer, perlnumber(1) AUTHOR
Andrew Main (Zefram) <zefram@fysh.org> COPYRIGHT
Copyright (C) 2007, 2009, 2010 Andrew Main (Zefram) <zefram@fysh.org> LICENSE
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2011-11-15 Scalar::Number(3pm)
All times are GMT -4. The time now is 10:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy