Divide returns the result of dividing the value by another value.
(other *Value)
| 165 | |
| 166 | // Divide returns the result of dividing the value by another value. |
| 167 | func (v *Value) Divide(other *Value) (*Value, error) { |
| 168 | if v.IsInt() && other.IsInt() { |
| 169 | a, err := v.IntValue() |
| 170 | if err != nil { |
| 171 | return nil, err |
| 172 | } |
| 173 | b, err := other.IntValue() |
| 174 | if err != nil { |
| 175 | return nil, err |
| 176 | } |
| 177 | return NewValue(a / b), nil |
| 178 | } |
| 179 | if v.IsFloat() && other.IsFloat() { |
| 180 | a, err := v.FloatValue() |
| 181 | if err != nil { |
| 182 | return nil, err |
| 183 | } |
| 184 | b, err := other.FloatValue() |
| 185 | if err != nil { |
| 186 | return nil, err |
| 187 | } |
| 188 | return NewValue(a / b), nil |
| 189 | } |
| 190 | if v.IsInt() && other.IsFloat() { |
| 191 | a, err := v.IntValue() |
| 192 | if err != nil { |
| 193 | return nil, err |
| 194 | } |
| 195 | b, err := other.FloatValue() |
| 196 | if err != nil { |
| 197 | return nil, err |
| 198 | } |
| 199 | return NewValue(float64(a) / b), nil |
| 200 | } |
| 201 | if v.IsFloat() && other.IsInt() { |
| 202 | a, err := v.FloatValue() |
| 203 | if err != nil { |
| 204 | return nil, err |
| 205 | } |
| 206 | b, err := other.IntValue() |
| 207 | if err != nil { |
| 208 | return nil, err |
| 209 | } |
| 210 | return NewValue(a / float64(b)), nil |
| 211 | } |
| 212 | return nil, fmt.Errorf("could not divide: %w", ErrIncompatibleTypes{A: v, B: other}) |
| 213 | } |
| 214 | |
| 215 | // Modulo returns the remainder of the division of two values. |
| 216 | func (v *Value) Modulo(other *Value) (*Value, error) { |