Sponsored Content
Top Forums Shell Programming and Scripting awk, floating point and rounding Post 302488922 by binlib on Tuesday 18th of January 2011 09:11:59 PM
Old 01-18-2011
There is nothing special about "(float)j". Awk interprets it as a concatenation of two strings. First the uninitialized variable "float" is treated as an empty string, then the variable j is converted to string using the default "%.6g" format.
Code:
awk 'BEGIN{for(x=0.1;x<1;x+=.1)printf("%.20f\n",x)}'
0.10000000000000000555
0.20000000000000001110
0.30000000000000004441
0.40000000000000002220
0.50000000000000000000
0.59999999999999997780
0.69999999999999995559
0.79999999999999993339
0.89999999999999991118
0.99999999999999988898

Here you can see that the last number is very close to 1. When it is converted to string using the default "%.6g", it will be 1 exactly.
 

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

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

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

4. Shell Programming and Scripting

Replace floating-point by integer in awk

Hi, I am trying to write a script to extract multiple sets of data from a chemistry output file. The problem section is in the following format... Geometry "geometry" -> "geometry" 1 Pd 46.0000 -0.19290971 0.00535260 0.02297606 2 P ... (7 Replies)
Discussion started by: smadonald1
7 Replies

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

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

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

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
SWFDISPLAYITEM(3)							 1							 SWFDISPLAYITEM(3)

The SWFDisplayItem class

INTRODUCTION
SWFDisplayItem. CLASS SYNOPSIS
SWFDisplayItem SWFDisplayItem Methods o void SWFDisplayItem::addAction (SWFAction $action, int $flags) o void SWFDisplayItem::addColor (int $red, int $green, int $blue, [int $a]) o void SWFDisplayItem::endMask (void ) o float SWFDisplayItem::getRot (void ) o float SWFDisplayItem::getX (void ) o float SWFDisplayItem::getXScale (void ) o float SWFDisplayItem::getXSkew (void ) o float SWFDisplayItem::getY (void ) o float SWFDisplayItem::getYScale (void ) o float SWFDisplayItem::getYSkew (void ) o void SWFDisplayItem::move (float $dx, float $dy) o void SWFDisplayItem::moveTo (float $x, float $y) o void SWFDisplayItem::multColor (float $red, float $green, float $blue, [float $a]) o void SWFDisplayItem::remove (void ) o void SWFDisplayItem::rotate (float $angle) o void SWFDisplayItem::rotateTo (float $angle) o void SWFDisplayItem::scale (float $dx, float $dy) o void SWFDisplayItem::scaleTo (float $x, [float $y]) o void SWFDisplayItem::setDepth (int $depth) o void SWFDisplayItem::setMaskLevel (int $level) o void SWFDisplayItem::setMatrix (float $a, float $b, float $c, float $d, float $x, float $y) o void SWFDisplayItem::setName (string $name) o void SWFDisplayItem::setRatio (float $ratio) o void SWFDisplayItem::skewX (float $ddegrees) o void SWFDisplayItem::skewXTo (float $degrees) o void SWFDisplayItem::skewY (float $ddegrees) o void SWFDisplayItem::skewYTo (float $degrees) PHP Documentation Group SWFDISPLAYITEM(3)
All times are GMT -4. The time now is 05:05 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy