LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2015, 05:21 AM   #1
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
more weird ruby arithmetic


More unexpected results for ruby arithmetic.
Standard integer arithmetic (not float)

Code:
irb(main):001:0> 1 / 5
=> 0
OK that is cool, what I expected, nice, I can dig it.
then...

Code:
irb(main):002:0> -1 / 5
=> -1
Wtf?

Is it me or has the world gone mad?

it gets worse!

Code:
irb(main):004:0> -7/5
=> -2

irb(main):005:0> -30/5
=> -6
irb(main):006:0> -31/5
=> -7
I mean, really!?

Last edited by bigearsbilly; 03-27-2015 at 05:41 AM.
 
Old 03-27-2015, 06:16 AM   #2
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
and in python it's the same, I mean, really!
here are the reasons
http://python-history.blogspot.de/20...on-floors.html

possible perl an option for you?

Last edited by a4z; 03-27-2015 at 06:18 AM.
 
Old 03-27-2015, 06:29 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: Exactly what is your problem/question?
 
Old 03-27-2015, 06:34 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
aah interesting article.

well, i generally use C and Perl but I like to use the ruby interactive interpreter for arithmetic and testing little functions. I am adjusting a C function and I use the irb to quickly mess about with it.

Strangely the ruby version worked for precisely those reasons.
I hate surprises.

edit:
thanks a lot I have solved my problem by implementing the floored division.

Last edited by bigearsbilly; 03-27-2015 at 06:46 AM.
 
Old 03-27-2015, 06:48 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by NevemTeve View Post
@OP: Exactly what is your problem/question?
not sure, some days the old brain doesn't quite work properly.
 
Old 03-27-2015, 08:32 AM   #6
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by bigearsbilly View Post
thanks a lot I have solved my problem by implementing the floored division.
implemented? it is already implemented
Code:
irb(main):017:0> -1.0/5
=> -0.2
irb(main):018:0> -1/5.0
=> -0.2
irb(main):019:0> -1/5.to_f
=> -0.2
irb(main):021:0> -1/5.to_f
=> -0.2
irb(main):022:0> -1.to_f/5.to_f
 
  


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
LXer: How to install Ruby and Ruby on Rails on CentOS LXer Syndicated Linux News 0 09-15-2014 07:30 PM
RVM + local install ruby + system ruby maintenance Corpus-Khu Linux - Software 0 02-13-2014 01:45 AM
arithmetic operation C++ very weird nushki Programming 19 06-03-2011 12:49 AM
Any issues installing Ruby Gems and Ruby on Rails in Slackware? Lufbery Slackware 8 02-09-2011 07:22 PM
weird problems faced while incorporating ruby and python! wrapster Programming 2 10-05-2008 06:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:32 PM.

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