Skip to content

Instantly share code, notes, and snippets.

@arakov
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arakov/0ba71636c01070e5e834 to your computer and use it in GitHub Desktop.
Save arakov/0ba71636c01070e5e834 to your computer and use it in GitHub Desktop.
alternative version
#define system.
#define extensions.
#class MyNumber
{
#field theValue.
#constructor new : aValue
[
theValue := aValue.
]
#method add : aValue
[
#var x := theValue.
#var y := aValue.
#loop (y != 0)?
[
#var carry := x && y.
x := x ^^ y.
y := carry shift &index:-1.
].
^ MyNumber new:x.
]
#method not
[
#var temp := MyNumber new:(theValue not).
^ MyNumber new:(temp + 1).
]
#method subtract : aValue
[
#var x := $self.
#var y := MyNumber new:(aValue) not.
^x + y.
]
#method multiply : aValue
[
#var x := $self.
#var y := aValue.
// x?x^1?add(y,mul(dec(x),y)):y:0;
]
#method divide : aValue
[
#var divisor := aValue.
#var dividendo := theValue.
#var temp := 1.
#var result := 0.
#var quociente := MyNumber new:(0).
#var dividendoTemp := MyNumber new:(dividendo).
#loop (divisor <= dividendo)?
[
divisor := divisor shift &index:-1.
temp := temp shift &index:-1.
].
#loop (temp > 1)?
[
divisor := divisor shift &index:1.
temp := temp shift &index:1.
(dividendoTemp >= divisor)?
[
dividendoTemp := dividendoTemp - divisor.
quociente := quociente + temp.
].
].
^quociente.
]
#method => theValue.
}
#symbol Program =
[
consoleEx writeLine:"Sum............: 20+20=":(MyNumber new:20 + 20).
//consoleEx writeLine:"Subtract.......: 30-30=":(MyNumber new:30 - 30).
consoleEx writeLine:"Not............: ~10=":((MyNumber new:10) not).
consoleEx writeLine:"Division.......: 36/06=":(MyNumber new:36 / 6).
consoleEx writeLine:"Division.......: 100/10=":(MyNumber new:100 / 10).
consoleEx readChar.
].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment