GIDForums  

Go Back   GIDForums > Computer Forums > Computer Software Forum - Linux
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Apr-2009, 10:51
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Floating point math on bash command line


For quick calculations I can do this on the linux command line:
Code:
cprogs> echo "'$PS1'" && lps=$PS1 && PS1=">> " ## <-- change prompt for demo '\W> ' >> echo $((3 * 333)) ## <-- Do a simple command line calculation 999
...but when it comes to floating point I get C style integer operations and results.
Code:
>> echo $(( 22 / 7 )) 3 >> echo $(( 22 % 7 )) 1
Is there a way to specify to use float or double?

I have seen that I can use bc (which is a powerful and very easy to use) :
Code:
>> bc -q 22 / 7 3 scale=4 22 / 7 3.1428 scale=20 22 / 7 3.14285714285714285714 quit >> # or like the example shown in the bc man page: >> >> echo "scale=20; 4*a(1)" | bc -l 3.14159265358979323844 >> PS1=$lps ## <-- change prompt back to original cprogs>
but still I wonder if bash provides a way to cast or print the result in floating point.
  #2  
Old 22-Apr-2009, 13:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: floating point math on bash command line


Quote:
Originally Posted by Howard_L
...I wonder if bash provides a way to cast or print the result in floating point.

No.

Regards,

Dave

Footnote: I'm thinking that bc is probably the "most popular" way, but you could also use GNU octave from bash (or other shell):
Code:
$ echo 'printf("%.16f\n",22/7)' | octave -q 3.1428571428571428 $
  #3  
Old 23-Apr-2009, 00:17
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Floating point math on bash command line


Ahh yes octave.
wow
Code:
3.14159265358979323846 wikipedia 3.1428571428571428 echo 'printf("%.16f\n",22/7)' | octave -q 3.14285714285714285714 echo "scale=20; 22 / 7" | bc 3.14159265358979323844 echo "scale=20; 4*a(1)" | bc -l
So I see that 22/7 is not that close as you said back in December

Now I wonder what a(1) is... and 4* ???

man bc:
MATH LIBRARY
a (x) The arctangent of x, arctangent returns radians.

boy I'm gonna have to get out a math book...

Thanks dave
  #4  
Old 23-Apr-2009, 10:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Floating point math on bash command line


Quote:
Originally Posted by Howard_L
Ahh yes octave.
I threw that in just for the heck of it because you expressed an interest in octave some time ago. GNU octave uses 64-bit floating point double precision variables, good for about 16 significant decimal digits. The bc program is a terrific "arbitrary precision" machine that can give as much precision as you specify with the "scale=..." statement.
Quote:
Originally Posted by Howard_L
...I see that 22/7 is not that close as you said ...

Refresh me: What the heck did I say back in December? (I mean, last Tuesday is ancient history to me; last December? Oh, man...)

Here's what I say now:

22/7 is an infinitely repeating decimal fraction. If you round off 22/7 to four significant decimal digits you get

3.143


If you round off pi to four significant decimal digits you get

3.142


If you round off 22/7 to three significant digits you get

3.14


If you round off pi to three significant digits you get

3.14


My conclusion is that 22/7 is an approximation to pi that is correct to three significant decimal digits.

Quote:
Originally Posted by Howard_L
Now I wonder what a(1) is... and 4* ???
a (x) The arctangent of x, arctangent returns radians.

Here's the reasoning behind that little jewel:

The arctangent function returns an angle whose tangent is given by the argument. In fact there are an infinite number of angles whose tangent is a given value. Most program implementations of the atan function return a value between -pi/2 and pi/2.
See, for example Inverse Tangent

Anyhow...

The tangent of pi/4 radians is 1

Therefore the arctangent of 1 is pi/4

Therefore, pi is equal to 4 times the arctangent of 1.

Of course, the arctangent function calculates an approximate value using whatever precision is available, so an approximation of pi can be obtained by multiplying arctangent(1) by four. Note that there is slight difference (two units in the 21st decimal digit) between pi and the approximation calculated by (4 times arctangent(1)). This is due to roundoff error/truncation error in the arctangent function evaluation being propagated to the product.


Regards,

Dave
  #5  
Old 23-Apr-2009, 13:27
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Floating point math on bash command line


Quote:
Refresh me: I mean, last Tuesday is ancient history
I hear that! , It was just this thread to nowhere...
Quote:
Quote:
...Pye used to be 22/7 (fractions).
No, it didn't, Sometimes teachers present 22/7 as being a simple rational approximation to pi.
gidforums.com/t-20056.html
Ring a bell?

re: octave
Yes, Thanks for the example. I was getting into it and then it got cold! ("what the heck?" you say)
Short story , This is an old farm house, we are using a wood stove exclusively. My fedora box is in the "Outback".
I shut the door a few months ago and used the my old laptop with linuxfromscratch in a warm room.
I didn't want to fool with building all the stuff for octave....
Now I'm back in here, but so many things to do...

I did build tlc and tk and learned to make a graphical plotting program though!!!: )
Very interesting to use tlc and see what the pro's do with object oriented programming.

Quote:
The bc program is a terrific "arbitrary precision" machine that can give as much precision as you specify with the "scale=..." statement.
Now that issss interesting.
This would be that "bank" quality numeric handling you have talked about in the past.

re: a (x)
What you explain makes sense and I almost understand.
What I really need to do in that department is start back at algebra and trig 1 and learn math!
I find I understand it better now than in high school ,, but still , a long way to go.

Thanks, Howard();
  #6  
Old 23-Apr-2009, 13:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Floating point math on bash command line


Quote:
Originally Posted by Howard_L
I hear that! , It was just this thread to nowhere...Ring a bell?

In that thread I said that 22/7 was correct to 3 significant decimal digits. What makes you say that your results are "not as close" as what I said?

Quote:
Originally Posted by Howard_L
... linuxfromscratch ...
What a terrific project! Regardless of the value of learning to build the kernel and tools and file system (and so on) from scratch, learning what actually happens when you boot up into Linux is simply priceless. It is also mind-boggling. (I'm thinking that this kind of boggling is a Good Thing, since it doesn't involve pharmaceuticals. Or as Archie McNally says from time to time, "It left me with my flabber totally gasted.") That's one time that I bought the book (a year or so ago) even though Open Source documentation is available.

Quote:
Originally Posted by Howard_L
This would be that "bank" quality numeric handling you have talked about in the past.
Well, to me "bank" quality arithmetic must use fixed point decimal arithmetic. You simply can't guarantee results down to the penny with binary/floating point arithmetic. Or, putting it another way, even if someone "guarantees" it, I'm not going for it.

Decimal arithmetic is available natively in COBOL, and I know that there are BCD class libraries for C++. Or---you can roll your own arbitrary precision decimal arithmetic package. How tough could that be for people who build Linux from scratch?


Regards,

Dave
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic 2 dimensional array problem mas287 C Programming Language 8 11-Feb-2009 18:06
Need a function to make chart branpute C Programming Language 10 03-Jan-2009 16:39
No type named nostaque C++ Forum 2 06-Nov-2008 16:46
How to exchange the Lower and Upper Nibbles of a floating point number vicky_brsh C++ Forum 2 15-May-2008 10:18
Linked list: Dereferencing to incomplete type shalombi C Programming Language 3 08-Jun-2007 15:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:07.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.