help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calculation


From: Silvio Siefke
Subject: Re: Calculation
Date: Sun, 20 Jun 2021 20:41:15 +0200

On Sun, 20 Jun 2021 12:56:32 -0400
Greg Wooledge <greg@wooledge.org> wrote:

> On Sun, Jun 20, 2021 at 06:35:39PM +0200, Silvio Siefke wrote:
> > At end I use a script for the job. The size of the construction sites 
> > are loaded in an array. With the sizes, the materials are calculated 
> > and at the end calculated together and also calculated on the square 
> > meter. All results are written to a CSV file.
> 
> > size.txt
> > 90.25
> > 50.58
> > 59.74
> > 12.80
> > 10.80
> > 98.58
> > 15.60
> > 39.36
> > 20.15
> > 825.69
> > 1080.96
> > 2087.69
> > 5074.80
> 
> OK, this is helpful.
> 
> > # the size read in to an array
> > readarray -t size < /home/siefke/Downloads/size.txt
> > 
> > # loop goes over the array and start calculate
> > for i in "${size[@]}"; do
> 
> You're never using more than one "size" value at a time, so the array
> isn't really necessary.  However, since the file is small (you said
> it's about 100 lines), holding it in memory is fine.

The size is export of my program for handling projects. More I need not, 
from the size I can calculate all I need.

> > # deciamal numbers round up to single number
> > round_days=$(concalc "$i"/150 | awk '{print 
> > ($0-int($0)<0.499)?int($0):int($0)+1}')
> > round_sand=$(concalc "$i*0.04*1,8" | awk '{print 
> > ($0-int($0)<0.499)?int($0):int($0)+1}')
> > round_cement=$(concalc "$i*0.04*15" | awk '{print 
> > ($0-int($0)<0.499)?int($0):int($0)+1}')
> > round_adhesive_bridge=$(concalc "$i*1.5/25" | awk '{print 
> > ($0-int($0)<0.499)?int($0):int($0)+1}')
> > round_edgestripes=$(concalc "$i*1.5/50" | awk '{print 
> > ($0-int($0)<0.499)?int($0):int($0)+1}')
> 
> You're still using this really convoluted awk command to do rounding,
> and the rounding you're doing isn't quite correct.  Nor does it match
> the comments.
> 
> The comment says "round up", but that's not what you're doing.  You're
> not even rounding normally, because for some reason you're treating 0.499
> as a value to be rounded up, rather than down.

Yes this I saw for salary calculation. Because when you have 190/150 = 
1,266666667, so
it say 1 but exactly would 2!

> Finally, I still have no idea what "concalc" does.  Is it simply performing
> the arithmetic specified in a single argument?  I.e. for the first one:
> 
> concalc "$i/150"

I can not handle bc it looks hard when looks in net. So I search alternative
calculator this is concalc.

http://extcalc-linux.sourceforge.net/concalcdescr.html

> Is it simply dividing $i by 150, and then writing the result to stdout?

$i mean size and 150 is daily output. That's why need to round it. 

> If that's the case, you can do the whole thing in awk, including the
> rounding, and do the rounding *properly* at the same time, with a lot
> less code.
> 
> awk '
>   {days = $1/150.0; sand = $1*0.04*1.8; cement = $1*0.04*15;
>    bridge = $1*1.5/25; edgestripes = $1*1.5/50;
>    printf("%.0f,%.0f,%.0f,%.0f,%.0f\n",
>      days, sand, cement, bridge, edgestripes)
>   }
> ' size.txt

That looks interesting. AWK seems to be a powerful tool indeed. Awk rounds up
how can still take the prices in there? 

days take we first Entry from size.txt: 
days: 90.25 / 150 = 0,601666667 = ~1 x (8/100) = 800
sand: 6,498 ~ 7 = 7to. x 25 = 175
cement: 90,25×0,04×15 = 54,15 = ~ 55 x 2 = 110
...

> Bash isn't actually helping you very much here, because it can't do
> floating point arithmetic.  So you might as well write the whole thing
> in awk, which can easily do everything required for this project.

This is what net also says. I have little bit read about python but hhh I 
be happy when it can run with bash and later try to handle it with other
language. 

Thank you 
Silvio

Attachment: pgp4w8OYzkOzs.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]