Newbie question: modulo operator with negative operand, bug or feature?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Newbie question: modulo operator with negative operand, bug or feature?
Prev   Next
# 1  
Old 08-11-2015
Newbie question: modulo operator with negative operand, bug or feature?

Hi,

I'm new to the Ash shell so my apologies if this is well known. In normal maths and other shells and languages I've used, the modulo operator always returns a positive remainder. For example see this discussion (first post so I can't hyperlink it):

http://math.stackexchange.com/questions/519845/modulo-of-a-negative-number

The Ash shell (and I am told Bash too) return a negative result. This code run on BusyBox illustrates the problem:

Code:
#!/bin/sh
echo "The result of A % B should be an integer in the range 0 to B-1"
echo "(9-7) % 5 = $(((9-7)%5)) (should be 2)."
echo "(7-9) % 5 = $(((7-9)%5)) (should be 3)."
exit 0

Is this accepted as just a 'feature' of the shells or is it a bug?
This User Gave Thanks to FleetFoot For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. What is on Your Mind?

Bug Reporting and Feature Request Tracking on GitHub

Dear All, I created a "new public repo" on GitHub (without the code for now but will post the code there as needed if anyone wants to fix a bug) You can report any forum bugs and report issues in the GitHub "Issues Tab" for this repo here: https://github.com/unixneo/unix.com.bugtracker ... (0 Replies)
Discussion started by: Neo
0 Replies

2. Shell Programming and Scripting

Assignment operator without operand

Does anyone know how this line in bash works? local gotbase= force= nicelevel corelimit local pid base= user= nice= bg= pid_file= local cgroup= These lines are part of the daemon function inside the "functions" file at /etc/init.d in RH. (3 Replies)
Discussion started by: Rameshck
3 Replies

3. Shell Programming and Scripting

Modulo calculation in shell script

hi i am writing a progrm to print the even numbers and the code which i am following is as follows #!/usr/bin/bash echo "enter a number a" read a if then echo "the number is even" else echo "the number is odd" fi ~ what is the mistake i am doing ...please tell me (3 Replies)
Discussion started by: harjinder
3 Replies

4. Shell Programming and Scripting

OR operator syntax question in AWK script

Hi guys, I confused about syntax used in OR script as follow: I have this sample file separated by "|" containing: January|Month No. 1 February|Month No. 2 March|Month No. 3 April|Month No. 4 May|Month No. 5 June|Month No. 6 July|Month No. 7 August|Month No. 8 September|Month No. 9... (11 Replies)
Discussion started by: cgkmal
11 Replies

5. Shell Programming and Scripting

perl newbie . &&..programming newbie (question 2)

Hello everyone, I am having to do a lot of perl scripting these days and I am learning a lot. I have this problem I want to move files from a folder and all its sub folders to one parent folder, they are all .gz files.. there is folder1\folder2\*.gz and there are about 50 folders... (1 Reply)
Discussion started by: xytiz
1 Replies

6. UNIX for Dummies Questions & Answers

UNIX newbie NEWBIE question!

Hello everyone, Just started UNIX today! In our school we use solaris. I just want to know how do I setup Solaris 10 not the GUI one, the one where you have to type the commands like ECHO, ls, pwd, etc... I have windows xp and I also have vmware. I hope I am not missing anything! :p (4 Replies)
Discussion started by: Hanamachi
4 Replies

7. Shell Programming and Scripting

syntax error: `-a' unexpected operator/operand in IF

When i tyr this, it gives me a syntax error...i tried removing quotes,removing spaces,replacing -eq with '='.. Can somebody suggest that is the problem? if ]; then (4 Replies)
Discussion started by: dba.admin2008
4 Replies

8. UNIX for Dummies Questions & Answers

Operator question

I need to find out if a variable contains a certain text string, then do something about it. Here is what I mean, but I don't know how to get a "contains" operator # We have volumes called: # /Volumes/BackupsA_K # /Volumes/BackupsL_Z # /Volumes/Backups_Admin # (could be more, etc)... (5 Replies)
Discussion started by: TheCrunge
5 Replies
Login or Register to Ask a Question