Modulo returns the remainder of the division of two values.
(other *Value)
| 214 | |
| 215 | // Modulo returns the remainder of the division of two values. |
| 216 | func (v *Value) Modulo(other *Value) (*Value, error) { |
| 217 | if v.IsInt() && other.IsInt() { |
| 218 | a, err := v.IntValue() |
| 219 | if err != nil { |
| 220 | return nil, err |
| 221 | } |
| 222 | b, err := other.IntValue() |
| 223 | if err != nil { |
| 224 | return nil, err |
| 225 | } |
| 226 | return NewValue(a % b), nil |
| 227 | } |
| 228 | if v.IsFloat() && other.IsFloat() { |
| 229 | a, err := v.FloatValue() |
| 230 | if err != nil { |
| 231 | return nil, err |
| 232 | } |
| 233 | b, err := other.FloatValue() |
| 234 | if err != nil { |
| 235 | return nil, err |
| 236 | } |
| 237 | return NewValue(math.Mod(a, b)), nil |
| 238 | } |
| 239 | if v.IsInt() && other.IsFloat() { |
| 240 | a, err := v.IntValue() |
| 241 | if err != nil { |
| 242 | return nil, err |
| 243 | } |
| 244 | b, err := other.FloatValue() |
| 245 | if err != nil { |
| 246 | return nil, err |
| 247 | } |
| 248 | return NewValue(math.Mod(float64(a), b)), nil |
| 249 | } |
| 250 | if v.IsFloat() && other.IsInt() { |
| 251 | a, err := v.FloatValue() |
| 252 | if err != nil { |
| 253 | return nil, err |
| 254 | } |
| 255 | b, err := other.IntValue() |
| 256 | if err != nil { |
| 257 | return nil, err |
| 258 | } |
| 259 | return NewValue(math.Mod(a, float64(b))), nil |
| 260 | } |
| 261 | return nil, fmt.Errorf("could not modulo: %w", ErrIncompatibleTypes{A: v, B: other}) |
| 262 | } |