Sponsored Content
Top Forums Shell Programming and Scripting Rounding off to the next whole number Post 302215713 by mirusnet on Thursday 17th of July 2008 04:21:22 AM
Old 07-17-2008
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rounding off using BC.

Hello again. I'm trying to use BC to calculate some numbers in a shell script. I want to have the numbers rounded off to 1 decimal place. for example: initsize=1566720 zipsize=4733 I'm trying to get the ratio between them. the equation is: (($initsize-$zipsize)/$initsize)*100 so... (3 Replies)
Discussion started by: noodlesoup
3 Replies

2. Shell Programming and Scripting

Rounding off decimals to the nearest number in PERL

Hi Guys, I am generating a statistical report , below is the snippet of the code : Now, $nSlices stands for the time duration,meaning,the statistics will be displayed for that particular time duration. Trouble is, for certain values of $totalTime (which is the end time - start time ), i... (9 Replies)
Discussion started by: rdlover
9 Replies

3. Linux

Rounding Script Help

I need some help with my rouding script. I have started pretty much from scratch and have no idea if its correct or even close but I have been trying and have gotten to this point. i keep getting syntax errors and im not sure what is wrong. Here is what I got let value=$1; while do let... (0 Replies)
Discussion started by: kingrj46
0 Replies

4. Shell Programming and Scripting

Rounding Script Help

I need some help with my rouding script. I have started pretty much from scratch and have no idea if its correct or even close but I have been trying and have gotten to this point. i keep getting syntax errors and im not sure what is wrong. Here is what I got let value=$1; while do let... (4 Replies)
Discussion started by: kingrj46
4 Replies

5. UNIX for Dummies Questions & Answers

Rounding a decimal

Hi, I am currently using tcsh I am trying to round a decimal number to the ten-thousandths place For instance: 1.23456 is rounded up towards 1.2346 I am not looking for truncation, but for rounding. Anyone know how to do this with awk or expr? Thanks (2 Replies)
Discussion started by: miniwheats
2 Replies

6. Shell Programming and Scripting

PERL - rounding fractional number

It seems that perl sprintf uses the round-to-even method: foreach my $i ( 0.5, 1.5, 2.5, 3.5 ) { printf "$i -> %.0f\n", $i; } __END__ 0.5 -> 0 1.5 -> 2 2.5 -> 2 3.5 -> 4 4.5 -> 4 Where we probably wants to use round-half-up, i.e. output should be as below: 0.5 -> 1 1.5 -> 2... (8 Replies)
Discussion started by: ganapati
8 Replies

7. Shell Programming and Scripting

Rounding number, but....

Dear Experts, I'm trying to find a way to round a number but in this way: 14367.577 ---> 14000 I used the following to round the number to the closer integer: echo $var|awk '{print int($1+0.5)}' and also: xargs printf "%1.0f" However, they don't work for my above... (9 Replies)
Discussion started by: Gery
9 Replies

8. UNIX for Dummies Questions & Answers

Rounding up to nearest whole number

Hi all of you, Would be great if you help me with how to round up to whole number from my input values like 2.99996,2.17890,3.00002,-2.3456,-2.7890 o/p should be like 3,2,3,-2,-3 thnks in adv!!!! regards (3 Replies)
Discussion started by: Indra2011
3 Replies

9. Shell Programming and Scripting

Rounding off to the nearest floating number

I have a number, which I want to convert into the nearest floating number upto two places after the decimal point. E.g. 1.2346 will become 1.23 but 1.2356 will become 1.24 . Similarly 0.009 will be 0.01 and 0.001 will be 0.00 or 0.0 (not 0, wnat to keep the decimal... (1 Reply)
Discussion started by: hbar
1 Replies

10. Shell Programming and Scripting

[awk] rounding a float number?

Heyas Trying to calculate the total size of a file by reading its bitrate. Code snippet: fs_expected() { # # Returns the expected filesize in bytes # pr_str() { ff=$(cat $TMP.info) d="${ff#*bitrate: }" echo "${d%%,*}" | $AWK '{print $1}' | head -n 1 } t_BYTERATE=$((... (9 Replies)
Discussion started by: sea
9 Replies
FENV(3) 						     Linux Programmer's Manual							   FENV(3)

NAME
feclearexcept, fegetexceptflag, feraiseexcept, fesetexceptflag, fetestexcept, fegetenv, fegetround, feholdexcept, fesetround, fesetenv, feupdateenv, feenableexcept, fedisableexcept, fegetexcept - floating-point rounding and exception handling SYNOPSIS
#include <fenv.h> int feclearexcept(int excepts); int fegetexceptflag(fexcept_t *flagp, int excepts); int feraiseexcept(int excepts); int fesetexceptflag(const fexcept_t *flagp, int excepts); int fetestexcept(int excepts); int fegetround(void); int fesetround(int rounding_mode); int fegetenv(fenv_t *envp); int feholdexcept(fenv_t *envp); int fesetenv(const fenv_t *envp); int feupdateenv(const fenv_t *envp); Link with -lm. DESCRIPTION
These eleven functions were defined in C99, and describe the handling of floating-point rounding and exceptions (overflow, zero-divide etc.). Exceptions The divide-by-zero exception occurs when an operation on finite numbers produces infinity as exact answer. The overflow exception occurs when a result has to be represented as a floating-point number, but has (much) larger absolute value than the largest (finite) floating-point number that is representable. The underflow exception occurs when a result has to be represented as a floating-point number, but has smaller absolute value than the smallest positive normalized floating-point number (and would lose much accuracy when represented as a denormalized number). The inexact exception occurs when the rounded result of an operation is not equal to the infinite precision result. It may occur whenever overflow or underflow occurs. The invalid exception occurs when there is no well-defined result for an operation, as for 0/0 or infinity - infinity or sqrt(-1). Exception handling Exceptions are represented in two ways: as a single bit (exception present/absent), and these bits correspond in some implementation- defined way with bit positions in an integer, and also as an opaque structure that may contain more information about the exception (per- haps the code address where it occurred). Each of the macros FE_DIVBYZERO, FE_INEXACT, FE_INVALID, FE_OVERFLOW, FE_UNDERFLOW is defined when the implementation supports handling of the corresponding exception, and if so then defines the corresponding bit(s), so that one can call exception handling functions, for exam- ple, using the integer argument FE_OVERFLOW|FE_UNDERFLOW. Other exceptions may be supported. The macro FE_ALL_EXCEPT is the bitwise OR of all bits corresponding to supported exceptions. The feclearexcept() function clears the supported exceptions represented by the bits in its argument. The fegetexceptflag() function stores a representation of the state of the exception flags represented by the argument excepts in the opaque object *flagp. The feraiseexcept() function raises the supported exceptions represented by the bits in excepts. The fesetexceptflag() function sets the complete status for the exceptions represented by excepts to the value *flagp. This value must have been obtained by an earlier call of fegetexceptflag() with a last argument that contained all bits in excepts. The fetestexcept() function returns a word in which the bits are set that were set in the argument excepts and for which the corresponding exception is currently set. Rounding mode The rounding mode determines how the result of floating-point operations is treated when the result cannot be exactly represented in the significand. Various rounding modes may be provided: round to nearest (the default), round up (towards positive infinity), round down (towards negative infinity), and round towards zero. Each of the macros FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, and FE_TOWARDZERO is defined when the implementation supports getting and setting the corresponding rounding direction. The fegetround() function returns the macro corresponding to the current rounding mode. The fesetround() function sets the rounding mode as specified by its argument and returns zero when it was successful. C99 and POSIX.1-2008 specify an identifier, FLT_ROUNDS, defined in <float.h>, which indicates the implementation-defined rounding behavior for floating-point addition. This identifier has one of the following values: -1 The rounding mode is not determinable. 0 Rounding is towards 0. 1 Rounding is towards nearest number. 2 Rounding is towards positive infinity. 3 Rounding is towards negative infinity. Other values represent machine-dependent, nonstandard rounding modes. The value of FLT_ROUNDS should reflect the current rounding mode as set by fesetround() (but see BUGS). Floating-point environment The entire floating-point environment, including control modes and status flags, can be handled as one opaque object, of type fenv_t. The default environment is denoted by FE_DFL_ENV (of type const fenv_t *). This is the environment setup at program start and it is defined by ISO C to have round to nearest, all exceptions cleared and a nonstop (continue on exceptions) mode. The fegetenv() function saves the current floating-point environment in the object *envp. The feholdexcept() function does the same, then clears all exception flags, and sets a nonstop (continue on exceptions) mode, if available. It returns zero when successful. The fesetenv() function restores the floating-point environment from the object *envp. This object must be known to be valid, for example, the result of a call to fegetenv() or feholdexcept() or equal to FE_DFL_ENV. This call does not raise exceptions. The feupdateenv() function installs the floating-point environment represented by the object *envp, except that currently raised exceptions are not cleared. After calling this function, the raised exceptions will be a bitwise OR of those previously set with those in *envp. As before, the object *envp must be known to be valid. RETURN VALUE
These functions return zero on success and nonzero if an error occurred. VERSIONS
These functions first appeared in glibc in version 2.1. CONFORMING TO
IEC 60559 (IEC 559:1989), ANSI/IEEE 854, C99, POSIX.1-2001. NOTES
Glibc Notes If possible, the GNU C Library defines a macro FE_NOMASK_ENV which represents an environment where every exception raised causes a trap to occur. You can test for this macro using #ifdef. It is only defined if _GNU_SOURCE is defined. The C99 standard does not define a way to set individual bits in the floating-point mask, for example, to trap on specific flags. glibc 2.2 supports the functions feenableexcept() and fedisableexcept() to set individual floating-point traps, and fegetexcept() to query the state. #define _GNU_SOURCE #include <fenv.h> int feenableexcept(int excepts); int fedisableexcept(int excepts); int fegetexcept(void); The feenableexcept() and fedisableexcept() functions enable (disable) traps for each of the exceptions represented by excepts and return the previous set of enabled exceptions when successful, and -1 otherwise. The fegetexcept() function returns the set of all currently enabled exceptions. BUGS
C99 specifies that the value of FLT_ROUNDS should reflect changes to the current rounding mode, as set by fesetround(). Currently, this does not occur: FLT_ROUNDS always has the value 1. SEE ALSO
feature_test_macros(7), math_error(7) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2008-08-11 FENV(3)
All times are GMT -4. The time now is 07:05 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy