Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Floating Point Math Mistake?

I am trying to calculate an aspect ratio for an image:

Log.d("img.width:", String.valueOf(img.getIntrinsicWidth()));
Log.d("img.height:", String.valueOf(img.getIntrinsicHeight()));
float aspect = ((float)img.getIntrinsicWidth()) / ((float)img.getIntrinsicWidth());
Log.d("aspect:", String.valueOf(aspect));

however this produces unexpected results:

img.width:   297
img.height:  167
aspect:      1.0

This seems like it has a simple answer, yet I can't figure it out.

like image 838
Alex Gittemeier Avatar asked Jul 08 '26 00:07

Alex Gittemeier


1 Answers

You're dividing width by width. Try substituting one of them with the height.

like image 137
Ignacio Vazquez-Abrams Avatar answered Jul 10 '26 13:07

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!