big.Rat/big.Int verbose function calls

Package math/big has types and functions for working with arbitrary precision integers and rational numbers. I was wondering if anyone knew why the function calls are so needlessly verbose. E.g. look at the docs on multiplying two rational numbers: https://golang.org/pkg/math/big/#Rat.Mul

Instead of multiplying the receiver with one parameter, you have to provide two parameters which are multiplied and the result is then written to the receiver. Why was this designed this way?

You might need both inputs after the fact, therefore you need to provide an area of memory that can take the result, but if you do not need the results afterward, you can use its memory area to store the result, effectively giving you the choice to avoid huge memory allocations where possible.

2 Likes

That’s a fair argument. Not quite my flavor but now I can live with my code looking the way it does.

rat := (&big.Rat{}).Mul(big.NewRat(tickCount, tickFrequency), big.NewRat(1000000, 1)) 
microsPassed := (&big.Int{}).Div(rat.Num(), rat.Denom()).Int64()

Thanks.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.