very_ants 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +78 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/demo +19 -0
- data/bin/setup +6 -0
- data/lib/very_ants/int.rb +72 -0
- data/lib/very_ants/version.rb +3 -0
- data/lib/very_ants.rb +36 -0
- data/very_ants.gemspec +32 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c88de8ff08cf8a609532f887931ea0d23e2e4890
|
4
|
+
data.tar.gz: 588fb9692c36c12cbe31fda56c09422827be4b6a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 48ab7874e3626ced85e40a8825077f9b794937f89389d8540007e3ec79d49698d7e8dd15f5ece542415326a5100eccb11f3b00d596a4a48e98e8bdc1c9cac9f0
|
7
|
+
data.tar.gz: f235b26142214707eec3741b85cc09392596176eef6651447ce20275cf5164406dd92d4bcb5aa191b354470a1bf531c8e857c456ff431f4d3b17399751511a73
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Saghm Rossi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# VeryAnts - probabilistic integer arithmetic for Ruby
|
2
|
+
|
3
|
+
VeryAnts is a library for replacing regular (i.e. deterministic) arithmetic
|
4
|
+
operations with equivalent (on average) probabilistic variants. This is done by
|
5
|
+
supplying a variance constant, which determines a range of possible results for
|
6
|
+
each operation. For instance, given a constant `c`, arithmetic `x + y` is
|
7
|
+
defined as:
|
8
|
+
|
9
|
+
```
|
10
|
+
sum := 0, chance := 1 / c
|
11
|
+
|
12
|
+
(c * y) times do
|
13
|
+
r := [new random number between 0 and 1]
|
14
|
+
|
15
|
+
if r < chance
|
16
|
+
sum := sum + x
|
17
|
+
|
18
|
+
return sum
|
19
|
+
```
|
20
|
+
|
21
|
+
So for `c = 3`, `x = 5`, `y = 2`, you start with `0`, and have a `1/3` chance
|
22
|
+
of adding `5` to it six different times. On average, this would occur twice,
|
23
|
+
which would give you 10, the correct answer. Another nice property is that for
|
24
|
+
`c = 1`, addition works completely normally.
|
25
|
+
|
26
|
+
All of the other arithmetic operators (`-`, `*`, `/`, `%`) have the same
|
27
|
+
two properties, namely that they give the correct result on average, and they
|
28
|
+
*always* give the correct result when `c = 1`. Additionally, they also have the
|
29
|
+
less-easy-to-define property that a higher `c` roughly correlates to higher
|
30
|
+
variance.
|
31
|
+
|
32
|
+
## Installation
|
33
|
+
|
34
|
+
Add this line to your application's Gemfile:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
gem 'very_ants'
|
38
|
+
```
|
39
|
+
|
40
|
+
And then execute:
|
41
|
+
|
42
|
+
$ bundle
|
43
|
+
|
44
|
+
Or install it yourself as:
|
45
|
+
|
46
|
+
$ gem install very_ants
|
47
|
+
|
48
|
+
## Usage
|
49
|
+
|
50
|
+
Just `require "very_ants"`, and watch the carnage!
|
51
|
+
|
52
|
+
## FAQ
|
53
|
+
|
54
|
+
### Why would you ever want to do this?
|
55
|
+
|
56
|
+
You wouldn't.
|
57
|
+
|
58
|
+
### Why did you make it then?
|
59
|
+
|
60
|
+
I came up with the idea as a joke when talking to my roommate, and I thought it
|
61
|
+
would be funny.
|
62
|
+
|
63
|
+
## Isn't this is pretty slow way to do arithmetic?
|
64
|
+
|
65
|
+
Is efficiency really the reason you think this is a bad idea?
|
66
|
+
|
67
|
+
### What does "Very Ants" mean?
|
68
|
+
|
69
|
+
It's a pun on the word "variance"; "vari-ance" sounds like "very ants". Also,
|
70
|
+
it behaves really weirdly, or put another way, "very antsy".
|
71
|
+
|
72
|
+
### ...I hate you
|
73
|
+
|
74
|
+
Worth it.
|
75
|
+
|
76
|
+
## License
|
77
|
+
|
78
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/demo
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "very_ants"
|
5
|
+
|
6
|
+
puts "normal arithmetic"
|
7
|
+
puts
|
8
|
+
puts "14 + 6 = #{14 + 6}"
|
9
|
+
puts "14 - 6 = #{14 - 6}"
|
10
|
+
puts "14 * 6 = #{14 * 6}"
|
11
|
+
puts "14 / 6 = #{14 / 6}"
|
12
|
+
puts
|
13
|
+
puts "very ansty arithmetic (c = 4)"
|
14
|
+
Fixnum::set_c(4)
|
15
|
+
puts
|
16
|
+
puts "14 + 6 = #{14 + 6}"
|
17
|
+
puts "14 - 6 = #{14 - 6}"
|
18
|
+
puts "14 * 6 = #{14 * 6}"
|
19
|
+
puts "14 / 6 = #{14 / 6}"
|
data/bin/setup
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
module VeryAnts
|
2
|
+
module Int
|
3
|
+
def plus(x, y, c)
|
4
|
+
helper(x, 1, y, c)
|
5
|
+
end
|
6
|
+
|
7
|
+
def minus(x, y, c)
|
8
|
+
helper(x, -1, y, c)
|
9
|
+
end
|
10
|
+
|
11
|
+
def mult(x, y, c)
|
12
|
+
helper(0, x, y, c)
|
13
|
+
end
|
14
|
+
|
15
|
+
def divide(x, y, c)
|
16
|
+
divide_helper(x, y, c).first
|
17
|
+
end
|
18
|
+
|
19
|
+
def mod(x, y, c)
|
20
|
+
divide_helper(x, y, c).last
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def helper(init, incr, y, c)
|
26
|
+
init = init
|
27
|
+
chance = Rational(1, c)
|
28
|
+
real_mult(c, y).times { init = real_plus(init, incr) if rand < chance }
|
29
|
+
init
|
30
|
+
end
|
31
|
+
|
32
|
+
def real_plus(x, y)
|
33
|
+
(x.to_f + y).to_i
|
34
|
+
end
|
35
|
+
|
36
|
+
def real_minus(x, y)
|
37
|
+
real_plus(x, -y)
|
38
|
+
end
|
39
|
+
|
40
|
+
def real_mult(x, y)
|
41
|
+
(x.to_f * y).to_i
|
42
|
+
end
|
43
|
+
|
44
|
+
def divide_helper(x, y, c)
|
45
|
+
raise ZeroDivisionError if y == 0
|
46
|
+
|
47
|
+
chance = Rational(1, c)
|
48
|
+
|
49
|
+
if y < 0
|
50
|
+
q, r = divide_helper(x, -y)
|
51
|
+
return [-q, r]
|
52
|
+
end
|
53
|
+
|
54
|
+
if x < 0
|
55
|
+
q, r = divide_helper(-x, y)
|
56
|
+
return [-q, 0] if r == 0
|
57
|
+
[-real_minus(q, 1), real_minus(y, r)]
|
58
|
+
end
|
59
|
+
|
60
|
+
q = 0
|
61
|
+
r = x
|
62
|
+
|
63
|
+
(real_mult(c, x.div(y))).times do
|
64
|
+
next if rand >= chance
|
65
|
+
q = real_plus(q, 1)
|
66
|
+
r = real_minus(r, y)
|
67
|
+
end
|
68
|
+
|
69
|
+
[q, r]
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/very_ants.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require "very_ants/int"
|
2
|
+
require "very_ants/version"
|
3
|
+
|
4
|
+
class Fixnum
|
5
|
+
include VeryAnts::Int
|
6
|
+
|
7
|
+
@@c = 1
|
8
|
+
|
9
|
+
def self.set_c(i)
|
10
|
+
@@c = i
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.get_c
|
14
|
+
@@c
|
15
|
+
end
|
16
|
+
|
17
|
+
def +(other)
|
18
|
+
plus(self, other, @@c)
|
19
|
+
end
|
20
|
+
|
21
|
+
def -(other)
|
22
|
+
minus(self, other, @@c)
|
23
|
+
end
|
24
|
+
|
25
|
+
def *(other)
|
26
|
+
mult(self, other, @@c)
|
27
|
+
end
|
28
|
+
|
29
|
+
def /(other)
|
30
|
+
divide(self, other, @@c)
|
31
|
+
end
|
32
|
+
|
33
|
+
def %(other)
|
34
|
+
mod(self, other, @@c)
|
35
|
+
end
|
36
|
+
end
|
data/very_ants.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'very_ants/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "very_ants"
|
8
|
+
spec.version = VeryAnts::VERSION
|
9
|
+
spec.authors = ["Saghm Rossi"]
|
10
|
+
spec.email = ["saghmrossi@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Probabilistic integer arithmetic library}
|
13
|
+
spec.description = %q{Redefines standard integer arithmetic operators to be probabilistic (but still return the correct result on average). Not meant to be used seriously.}
|
14
|
+
spec.homepage = "https://github.com/saghm/very-ants"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
if spec.respond_to?(:metadata)
|
18
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
19
|
+
else
|
20
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
21
|
+
end
|
22
|
+
|
23
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
29
|
+
spec.add_development_dependency "pry", "~> 0.10"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: very_ants
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Saghm Rossi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Redefines standard integer arithmetic operators to be probabilistic (but
|
70
|
+
still return the correct result on average). Not meant to be used seriously.
|
71
|
+
email:
|
72
|
+
- saghmrossi@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/demo
|
86
|
+
- bin/setup
|
87
|
+
- lib/very_ants.rb
|
88
|
+
- lib/very_ants/int.rb
|
89
|
+
- lib/very_ants/version.rb
|
90
|
+
- very_ants.gemspec
|
91
|
+
homepage: https://github.com/saghm/very-ants
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata:
|
95
|
+
allowed_push_host: https://rubygems.org
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.5.2
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Probabilistic integer arithmetic library
|
116
|
+
test_files: []
|