LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-16-2010, 07:00 AM   #1
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
bc in bash


echo 1/2 | bc -l = .50000000000000000000

Its a huge waste of space. Is there anyway I can change that to float i.e output = .500000
 
Old 11-16-2010, 07:01 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Code:
sasha@reactor: echo "scale=6; 1/2" | bc -l
.500000
sasha@reactor:
 
Old 11-16-2010, 07:24 AM   #3
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by GrapefruiTgirl View Post
Code:
sasha@reactor: echo "scale=6; 1/2" | bc -l
.500000
sasha@reactor:
Is there anyway I could actually make bc calculate in float rather than skim the output by echo? The main reason I want to calculate in float is because bc is taking too long to calculate on the large amount of data that I have and it might take shorter time calculating if i calculate in float. Anyway thanks for your solution I learned something new .
 
Old 11-16-2010, 07:50 AM   #4
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,923
Blog Entries: 44

Rep: Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158Reputation: 3158
Hi,

Links 4,5 & 6 below will provide some useful insight and understanding.

Just a few links to aid you to gaining some understanding;


1 Linux Documentation Project
2 Rute Tutorial & Exposition
3 Linux Command Guide
4 Bash Beginners Guide
5 Bash Reference Manual
6 Advanced Bash-Scripting Guide
7 Linux Newbie Admin Guide
8 LinuxSelfHelp
9 Getting Started with Linux

The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!
 
Old 11-16-2010, 08:03 AM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Hi,

I'm not 100% sure I follow your last post.

We aren't using echo to "skim the output" here - we're giving `bc` an expression to evaluate, and telling it at what scale to do the evaluation.

About "float" or not.. Perhaps consulting the `bc` man page, near the beginning, in the section titled "NUMBERS", would answer your question about floats? I can't provide anything further than what it says there. But I believe using 'scale' is the way to achieve what you want as far as output. Does it make internal calculations any faster? I don't know, but I don't think so. A snippet from my bc (version 1.06.95) man page:
Code:
All numbers are represented internally in decimal and all computation is done in decimal.
This version truncates results from divide and multiply operations.
It is the second line which makes me suspect that 'scale' does not make calculations any faster, because to me, this line says bc is merely truncating the results of computations resulting in more places after the decimal than we want. Heh, 'scale' might even make it take longer

Sorry if I haven't quite answered your question; maybe someone else will have more to add.

Good luck!
 
Old 11-16-2010, 08:16 AM   #6
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Rep: Reputation: 234Reputation: 234Reputation: 234
Instead of bc you could use Perl to perform calculations:

Code:
alias c='perl -e '\''$_="@ARGV"; print eval $_, "\n"'\'''
Put the above alias into your ~/.bashrc to perform calculations in the command line or put it into the script to use it in the script.

In the thread Using Perl to perform calculations in Bash you’ll find the script explaining the possibilities of that alias.
 
Old 11-16-2010, 08:53 AM   #7
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by GrapefruiTgirl View Post
Hi,

I'm not 100% sure I follow your last post.

We aren't using echo to "skim the output" here - we're giving `bc` an expression to evaluate, and telling it at what scale to do the evaluation.

About "float" or not.. Perhaps consulting the `bc` man page, near the beginning, in the section titled "NUMBERS", would answer your question about floats? I can't provide anything further than what it says there. But I believe using 'scale' is the way to achieve what you want as far as output. Does it make internal calculations any faster? I don't know, but I don't think so. A snippet from my bc (version 1.06.95) man page:
Code:
All numbers are represented internally in decimal and all computation is done in decimal.
This version truncates results from divide and multiply operations.
It is the second line which makes me suspect that 'scale' does not make calculations any faster, because to me, this line says bc is merely truncating the results of computations resulting in more places after the decimal than we want. Heh, 'scale' might even make it take longer

Sorry if I haven't quite answered your question; maybe someone else will have more to add.

Good luck!

After doing all the research and finding facts about bc for 1 whole hour you state the exact facts that I found out I shoulda checked your post beforehand. Might have saved me a lot of time. Thank you sooooo much for all your help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 01:14 PM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration