Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add floating-point precision to four decimal places #1145

Closed
wants to merge 1 commit into from
Closed

add floating-point precision to four decimal places #1145

wants to merge 1 commit into from

Conversation

ahaooahaz
Copy link

Fixes Or Enhances

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

@ahaooahaz ahaooahaz requested a review from a team as a code owner August 16, 2023 15:01
@coveralls
Copy link

Coverage Status

coverage: 74.001% (+0.01%) from 73.991% when pulling b71b501 on ahaooahaz:feature/float into 5bf55dc on go-playground:master.

@deankarn
Copy link
Contributor

@ahaooahaz Thank you for the PR, but I already have one up that should correct the issue without truncating the decimal precision #1146

Will try to get it merged and published after work.

@ahaooahaz
Copy link
Author

@deankarn Floating-point numbers that lose precision should be a problem caused by reflection, I think the problem appears in validator.go:77 current.Field(f.idx), if use
current.Field(f.idx).Float() it already lose precision ,I saw your code modify the 32-bit and 64-bit tool functions from string to float respectively, but this is not the actual cause of the problem

@deankarn
Copy link
Contributor

@ahaooahaz there is actually no problem per se.

This is a floating point precision thing and because the float32 is returned as a float64 it's actuating gaining precision.

Here's why my other PR fixes the issue without number truncation which can introduce other issues, especially where the code was implemented.
The parsing code has been updated using asFloat32 and asFloat64 making it now behave identically to reflect.Float() function which both check the bit precision aka float32 vs float64

func asFloat64(param string) float64 {
	i, _ := strconv.ParseFloat(param, 64)
	return i
}

func asFloat32(param string) float64 {
	i, _ := strconv.ParseFloat(param, 32)
	return i
}

func main() {

	f32 := float32(0.1)
	f64 := float64(0.1)

	fmt.Println(float64(f32), reflect.ValueOf(f32).Float(), asFloat32("0.1"))
	fmt.Println(f64, reflect.ValueOf(f64).Float(), asFloat64("0.1"))
        //
	// output
	//
	// 0.10000000149011612 0.10000000149011612 0.10000000149011612
	// 0.1 0.1 0.1
}

@ahaooahaz
Copy link
Author

@deankarn ohhh, I get it, GREAT WORKS!!

@ahaooahaz ahaooahaz closed this Aug 17, 2023
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.

None yet

3 participants