2

Edit

As mentioned by chepner in the comments below, I naively misunderstood the bc programming and actually can view the created directories with

ls -a

This problem was solved elsewhere (link below), but I thought I'd include my own problem to help round out the question with background context. One can also just skip to the question further below.

Background

I have the following:

#!/bin/bash


##############################################################################

odir=$PWD

for (( i=0; i<=5; i++ )) ;
do

R=$(echo "scale=2;0.8+(0.1*$i)" |bc -l)

R1=$(echo "scale=2;0.5*($R)" |bc -l)

mkdir -p $odir/$R/rhf/
cd $odir/$R/rhf/

cp $odir/x.inp $odir/run

done

My output creates directories:

1.0 1.1 1.2 1.3

Directories 0.8 and 0.9 are not made. Therefore my problem involves the variable R:

    R=$(echo "scale=2;0.8+(0.1*$i)" |bc -l)

Directories with a "leading zero" (0.8 and 0.9) are not being printed.

Solution

This problem was solved in another question here.

In particular, for those interested in the solution, adapted from the second response in the link from potong, is:

 R=$(echo "scale=2;x=0.1 + 0.1; if(x<1) print 0; x" | bc

Question

I was wondering if there's any particular reason why bc cannot print the leading zero. Is it a bug, or does it result from something more fundamental? As a nooby in programming, is this error common and something I can expect to encounter again in the future?

Thank you.

Community
  • 1
  • 1
Blaisem
  • 557
  • 8
  • 17
  • Because why would it? – 123 Dec 06 '16 at 11:56
  • It prints greater than 1.0. Why would it refuse values less than 1.0? – Blaisem Dec 06 '16 at 12:05
  • If you need to have a list of consecutive numbers, you could use `seq 0.8 0.1 1.3` – oliv Dec 06 '16 at 12:07
  • 1
    Note that the directories were created, but since they begin with a `.`, they are hidden. Use `ls -a` to see that they do, indeed, exist. – chepner Dec 06 '16 at 12:12
  • 1
    I would probably consider it a bug. Standard practice is to print the leading zero. There's probably some implementation detail to explain why bc doesn't do that. – ccarton Dec 06 '16 at 13:48
  • @Blaisem It prints them when more than 1.0 because they are significant digits, the 0 before the decimal place is not. Definitely not a bug. – 123 Dec 06 '16 at 14:01
  • @ccarton Standard practice in what? – 123 Dec 06 '16 at 14:02
  • 1
    @123 It's the standard in both math and every other programming language. Try `print 0.4` in either python or perl for an example. Also, the leading 0 of a decimal less than 1 is not considered when counting significant digits; it's always printed. – ccarton Dec 06 '16 at 14:37
  • @ccarton Find me the posix spec that it is standard. – 123 Dec 06 '16 at 14:57
  • 1
    @123 posix is not relevant. The mathematical standards for decimal notation have existed for centuries. The leading 0 is not dropped. Just because it's not in posix doesn't mean that you can do it any way you please. Posix also doesn't define what multiplication means but that doesn't mean each language invents it's own version. – ccarton Dec 06 '16 at 16:22
  • @ccarton Not a universal mathematical standard. Just because you are used to something doesn't make it a standard. `Posix also doesn't define what multiplication means but that doesn't mean each language invents it's own version`, thats because they would be useless. Changing functionality is completely different than changing the format of unspecified output. – 123 Dec 07 '16 at 08:08

0 Answers0