Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created February 24, 2017 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waynejo/a4ebf11f379635012c31e7ce7f07afcb to your computer and use it in GitHub Desktop.
Save waynejo/a4ebf11f379635012c31e7ce7f07afcb to your computer and use it in GitHub Desktop.
object Main {
def AND(x1:Double, x2:Double): Double = {
val w1 = 0.5
val w2 = 0.5
val b = -0.7
val tmp = x1 * w1 + x2 * w2 + b
if (tmp <= 0) {
0
} else {
1
}
}
def NAND(x1:Double, x2:Double): Double = {
val w1 = -0.5
val w2 = -0.5
val b = 0.7
val tmp = x1 * w1 + x2 * w2 + b
if (tmp <= 0) {
0
} else {
1
}
}
def OR(x1:Double, x2:Double): Double = {
val w1 = 0.5
val w2 = 0.5
val b = -0.3
val tmp = x1 * w1 + x2 * w2 + b
if (tmp <= 0) {
0
} else {
1
}
}
def XOR(x1:Double, x2:Double): Double = {
val s1 = NAND(x1, x2)
val s2 = OR(x1, x2)
AND(s1, s2)
}
def main(args:Array[String]) {
println(XOR(0, 0))
println(XOR(1, 0))
println(XOR(0, 1))
println(XOR(1, 1))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment