Fun Stuff > CLIKC

A programming thread!

<< < (34/37) > >>

Loki:
What'd you need the third ternary for? Is there any case where !(a&lt;b) &amp;&amp; !(a&gt;b) &amp;&amp; !(a=b)?

osaka:
Consistency. Technically, with the first two you've covered all possible outputs, but you have to define what happens when they're equal.

As a fun fact, that max(a,b) was actually a #define sentence made by a friend for our Operating Systems course. Knowing him, he probably didn't do the third part and either the compiler threw an error or the code segfaulted. So he added the third part.

smack that isaiah:
Am I missing something, or couldn't a max function be done with just one ternary operation?

#define MAX(a, b) ((a > b) ? a : b)

if a is bigger than b, use a; otherwise use b (the otherwise necessarily being "if b >= a")

I think the main reason to use the more fleshed out version that osaka has posted would be if you're using objects which have overloaded the <, >, and == operators where it may not follow that {!(a > b) == (a <= b)} (imo, this means that these operators should never have been overloaded in the first place...).  Since this would be a #define, the types of a and b are not limited to int/double/float/etc.

osaka:
Indeed it could be done. Once again, this was for Operating Systems, and things tended to get a bit overdone just to make sure.

Masterpiece:
I have a problem, and was wondering whether you could help me out:

I have a method that is supposed to return a scaled instance of a Buffered Image I feed it. Since the program needs two different scaled versions of the same image, I first determine a scaling factor for the individual images and then feed that scaling factor to a general scaling method.

I know the scaling works, I also know that the methods propagate as needed. But the methods that are supposed to calculate the scaling factors always return zero. I have no idea why that happens, if I explicitly tell it that scale = 1.0, it works, but if I try to calculate scale as follows, scale will always be zero.


--- Code: ---private ImageIcon scaleFiltered(BufferedImage filtered){

double scale = (screenHeight - HEIGHT_SCREEN_PADDING) / filtered.getHeight();

// scale = 0.5;

return scaleImage(filtered, scale);

}
--- End code ---

any thoughts?

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version