Skip to content

feat(div): add MustDiv#44

Merged
quagmt merged 2 commits into
quagmt:masterfrom
vhlebnikov:feat-must-div
Feb 25, 2026
Merged

feat(div): add MustDiv#44
quagmt merged 2 commits into
quagmt:masterfrom
vhlebnikov:feat-must-div

Conversation

@vhlebnikov

@vhlebnikov vhlebnikov commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

It will be very helpful to have such function for big calculation chains, when we sure not dividing by zero

Example (where i need this method):

var duration float64

const hundred = 100.0
roundedDuration, _ := udecimal.MustFromFloat64(value).Div(udecimal.MustFromFloat64(hundred))
roundedDuration = roundedDuration.RoundHAZ(2)
floatRoundedDur := roundedDuration.InexactFloat64()
duration = &floatRoundedDur

And instead of this, I could do:

var duration float64

const hundred = 100.0
roundedDuration := udecimal.MustFromFloat64(value).
		MustDiv(udecimal.MustFromFloat64(hundred)).
		RoundHAZ(2).
		InexactFloat64()
duration = &roundedDuration

@quagmt

quagmt commented Feb 25, 2026

Copy link
Copy Markdown
Owner

can you add a test for this new method and an example in doc_test.go

@codecov

codecov Bot commented Feb 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.89%. Comparing base (3259bfe) to head (d6ff0ab).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #44      +/-   ##
==========================================
+ Coverage   96.41%   96.89%   +0.48%     
==========================================
  Files           5        5              
  Lines        1924     1481     -443     
==========================================
- Hits         1855     1435     -420     
+ Misses         46       23      -23     
  Partials       23       23              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@xuyang2

xuyang2 commented Feb 25, 2026

Copy link
Copy Markdown

how about add a Must function?

func Must(d Decimal, err error) Decimal {
	if err != nil {
		panic(err)
	}
	return d
}

see also:

@vhlebnikov

Copy link
Copy Markdown
Contributor Author

@xuyang2 I think it will add complexity to the code.

For example, to call Must function you need to prepend call with package name and also wrap all (probably) big line inside, it will be udecimal.Must(...longCalculaationLine...), instead of longCalclulationLine.MustDiv(y).

Just hard to read.

@vhlebnikov

Copy link
Copy Markdown
Contributor Author

@quagmt done, check please

@quagmt quagmt merged commit dfdef5e into quagmt:master Feb 25, 2026
9 checks passed
@quagmt

quagmt commented Feb 25, 2026

Copy link
Copy Markdown
Owner

Released this in v1.10.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants