github.com/filecoin-project/specs-actors/v4@v4.0.2/actors/util/math/ln_test.go (about)

     1  package math_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/filecoin-project/go-state-types/big"
     8  	"github.com/filecoin-project/specs-actors/v4/actors/util/math"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestNaturalLog(t *testing.T) {
    14  	lnInputs := math.Parse([]string{
    15  		"340282366920938463463374607431768211456",                       // Q.128 format of 1
    16  		"924990000000000000000000000000000000000",                       // Q.128 format of e (rounded up in 5th decimal place to handle truncation)
    17  		"34028236692093846346337460743176821145600000000000000000000",   // Q.128 format of 100e18
    18  		"6805647338418769269267492148635364229120000000000000000000000", // Q.128 format of 2e22
    19  		"204169000000000000000000000000000000",                          // Q.128 format of 0.0006
    20  		"34028236692093846346337460743",                                 // Q.128 format of 1e-10
    21  	})
    22  
    23  	expectedLnOutputs := math.Parse([]string{
    24  		"0", // Q.128 format of 0 = ln(1)
    25  		"340282366920938463463374607431768211456",   // Q.128 format of 1 = ln(e)
    26  		"15670582109617661336106769654068947397831", // Q.128 format of 46.051... = ln(100e18)
    27  		"17473506083804940763855390762239996622013", // Q.128 format of  51.35... = ln(2e22)
    28  		"-2524410000000000000000000000000000000000", // Q.128 format of -7.41.. = ln(0.0006)
    29  		"-7835291054808830668053384827034473698915", // Q.128 format of -23.02.. = ln(1e-10)
    30  	})
    31  	fmt.Printf("%v %v\n", lnInputs, expectedLnOutputs)
    32  	require.Equal(t, len(lnInputs), len(expectedLnOutputs))
    33  	for i := 0; i < len(lnInputs); i++ {
    34  		z := big.NewFromGo(lnInputs[i])
    35  		lnOfZ := math.Ln(z)
    36  		expectedZ := big.NewFromGo(expectedLnOutputs[i])
    37  		assert.Equal(t, big.Rsh(expectedZ, math.Precision128), big.Rsh(lnOfZ, math.Precision128), "failed ln of %v", z)
    38  	}
    39  }