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

Cast to float may be needed #4210

Closed
scuniff opened this issue Jul 21, 2024 · 0 comments · Fixed by #4220
Closed

Cast to float may be needed #4210

scuniff opened this issue Jul 21, 2024 · 0 comments · Fixed by #4220

Comments

@scuniff
Copy link

scuniff commented Jul 21, 2024

float dist = (ints[i][j] - min) / diff;

Since ints[][], min, and diff are all ints, the equation is done with Integer accuracy and not Float accuarcy.

To get Float accuracy from the equation, a float cast is needed

Maybe
float dist = (float) (ints[i][j] - min) / diff;

Example:

	int ints[][]= new int[1][1];	
	int min, diff;
	
	diff = 100;
	min = 20;
	ints[0][0] = 40;
	
             // As is, no cast
	float dist = (ints[0][0] - min) /diff;		
	System.out.println("dist without cast is " + dist);		
	

            // With cast to float	
	dist = (float) (ints[0][0] - min) /diff;
	System.out.println("dist with cast is " + dist);		

Output:

dist without cast is 0.0
dist with cast is 0.2

melissalinkert added a commit to melissalinkert/bioformats that referenced this issue Aug 6, 2024
sbesson added a commit that referenced this issue Sep 3, 2024
Fix float casting when autoscaling int values to byte
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 a pull request may close this issue.

1 participant