LinuxQuestions.org
Review your favorite Linux distribution.
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 04-05-2012, 10:22 PM   #1
dileepmani
LQ Newbie
 
Registered: Mar 2012
Location: Kerala, India
Distribution: Ubuntu
Posts: 2

Rep: Reputation: Disabled
Unhappy problem with bash calculation


why is
echo $((1111111111111111111111111%10))
=3
and not 1

I was writing a blog on Fibonacci series ( http://dileepmani19.blogspot.in/2012...i-numbers.html ) and above problem was only solved by
echo "1111111111111111111111111%10" | bc
=1

Last edited by dileepmani; 04-05-2012 at 10:30 PM.
 
Old 04-05-2012, 11:04 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
There are limits to the values that can be used in bash--as with every other program.

The bash man page states:
Code:
ARITHMETIC EVALUATION
       The  shell allows arithmetic expressions to be evaluated, under certain
       circumstances (see the let and declare builtin commands and  Arithmetic
       Expansion).   Evaluation  is done in fixed-width integers with no check
       for overflow, though division by 0 is trapped and flagged as an  error.
I did not see a reference to the exact size of integer bash supports.

However, integers are typically 32 or 64 bits wide. In either case, your string of ones exceeds the value capable of being represented by either. The maximum value a 64-bit integer can represent (without being concerned with negative values) is 18,446,744,073,709,551,615.

For comparison:
Code:
your string of ones:             1,111,111,111,111,111,111,111,111
maximum unsigned 64-bit integer:        18,446,744,073,709,551,615
Your value very likely exceeds the maximum value bash is capable of handling, bash does not signal an "overflow" when you try to use it (as mentioned in the man page above), and so, the number of bits needed to represent your value that exceed bash's internal storage are stripped/dropped/truncated. Your script would be left with a number completely different than you would expect.

The bc program likely uses extra code and/or larger data sizes that can accommodate your value.

Note: the maximum value I showed for a 64-bit integer above is for an unsigned integer. For an integer that can represent both positive and negative values, the maximum value would be reduced by approximately half.

EDIT:
After further research and experimentation, on my machine, the maximum range of integer values bash can represent (on my machine):
Code:
[-9,223,372,036,854,775,808  <-->  +9,223,372,036,854,775,807]
Which corresponds to a signed 64-bit integer.

Shell session:
Code:
user@localhost$ limitcheck=9223372036854775807
user@localhost$ echo $limitcheck
9223372036854775807
user@localhost$ let limitcheck=limitcheck+1
user@localhost$ echo $limitcheck
-9223372036854775808

Last edited by Dark_Helmet; 04-05-2012 at 11:33 PM.
 
Old 04-05-2012, 11:34 PM   #3
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
bash uses long.
It does NOT check for overflows and wraps around starting then with LLONG_MIN.

So if you do let the bash calculate numbers greater than a LONG, it will give wrong results.

In the second case the bash did no calculating at all.
It just gave a string to bc.
 
Old 04-06-2012, 05:13 AM   #4
dileepmani
LQ Newbie
 
Registered: Mar 2012
Location: Kerala, India
Distribution: Ubuntu
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks a lot. Your explanations are very lucid.

Last edited by dileepmani; 04-06-2012 at 05:15 AM.
 
Old 04-06-2012, 01:29 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Please mark as SOLVED once you have a solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
[SOLVED] useradd problem with UID calculation rylphs Linux - Newbie 5 08-30-2010 03:48 PM
[SOLVED] BASH: Download Script -> While Loop Calculation Toubles... Devcon Programming 5 07-29-2010 01:41 AM
problem with process time calculation aloktiagi Linux - Newbie 3 08-11-2009 12:05 PM
Simple bash calculation Hiperi0n Programming 12 11-14-2008 08:10 AM


All times are GMT -5. The time now is 04:00 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration