Bitwise operations on floats actually arent useful
This commit is contained in:
parent
afe0ed33ca
commit
c64cda0870
87
calculate.go
87
calculate.go
@ -183,7 +183,6 @@ func (operation *Operation) solution () (solution int64, err error) {
|
||||
|
||||
func (operation *Operation) inexactSolution () (solution float64, err error) {
|
||||
var subSolution float64
|
||||
var rawSolution uint64
|
||||
|
||||
switch operation.opcode {
|
||||
case OpcodeAdd:
|
||||
@ -271,88 +270,10 @@ func (operation *Operation) inexactSolution () (solution float64, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
case OpcodeOr:
|
||||
for _, operand := range operation.operands {
|
||||
subSolution, err = operand.InexactSolution()
|
||||
if err != nil { return }
|
||||
rawSolution |= math.Float64bits(subSolution)
|
||||
}
|
||||
solution = math.Float64frombits(rawSolution)
|
||||
|
||||
case OpcodeNot:
|
||||
if len(operation.operands) != 1 {
|
||||
err = ErrorWrongOperandCount
|
||||
return
|
||||
}
|
||||
|
||||
subSolution, err = operation.operands[0].InexactSolution()
|
||||
solution = math.Float64frombits(^math.Float64bits(subSolution))
|
||||
|
||||
case OpcodeAnd:
|
||||
for index, operand := range operation.operands {
|
||||
subSolution, err = operand.InexactSolution()
|
||||
if err != nil { return }
|
||||
if index == 0 {
|
||||
rawSolution = math.Float64bits(subSolution)
|
||||
} else {
|
||||
rawSolution &= math.Float64bits(subSolution)
|
||||
}
|
||||
}
|
||||
solution = math.Float64frombits(rawSolution)
|
||||
|
||||
case OpcodeXor:
|
||||
for index, operand := range operation.operands {
|
||||
subSolution, err = operand.InexactSolution()
|
||||
if err != nil { return }
|
||||
if index == 0 {
|
||||
rawSolution = math.Float64bits(subSolution)
|
||||
} else {
|
||||
rawSolution ^= math.Float64bits(subSolution)
|
||||
}
|
||||
}
|
||||
solution = math.Float64frombits(rawSolution)
|
||||
|
||||
case OpcodeLeftShift:
|
||||
if len(operation.operands) != 2 {
|
||||
err = ErrorWrongOperandCount
|
||||
return
|
||||
}
|
||||
|
||||
var left, right float64
|
||||
left, err = operation.operands[0].InexactSolution()
|
||||
if err != nil { return }
|
||||
right, err = operation.operands[1].InexactSolution()
|
||||
if err != nil { return }
|
||||
|
||||
if right < 0 {
|
||||
err = ErrorNegativeShiftAmount
|
||||
return
|
||||
}
|
||||
|
||||
solution = math.Float64frombits (
|
||||
math.Float64bits(left) <<
|
||||
math.Float64bits(right))
|
||||
|
||||
case OpcodeRightShift:
|
||||
if len(operation.operands) != 2 {
|
||||
err = ErrorWrongOperandCount
|
||||
return
|
||||
}
|
||||
|
||||
var left, right float64
|
||||
left, err = operation.operands[0].InexactSolution()
|
||||
if err != nil { return }
|
||||
right, err = operation.operands[1].InexactSolution()
|
||||
if err != nil { return }
|
||||
|
||||
if right < 0 {
|
||||
err = ErrorNegativeShiftAmount
|
||||
return
|
||||
}
|
||||
|
||||
solution = math.Float64frombits (
|
||||
math.Float64bits(left) >>
|
||||
math.Float64bits(right))
|
||||
case
|
||||
OpcodeOr, OpcodeNot, OpcodeAnd, OpcodeXor, OpcodeLeftShift,
|
||||
OpcodeRightShift:
|
||||
err = ErrorWrongType
|
||||
|
||||
case OpcodeMean:
|
||||
if len(operation.operands) == 0 { break }
|
||||
|
Loading…
Reference in New Issue
Block a user