June 6, 200917 yr Commercial Member plane_heading = PLANE_HEADING_DEGREES_MAGNETICvar.var_value.n;I'd like to round this value up.I'm finding that Heading value in the FSX default aircraft are being rounded up.So for example, in the default 737 the heading could read 250.But when I return plane heading as a string to see what it's doing, I'll get 249.So how do I round the value up?I tried using ROUND, but VS didn't like that too much.Cheers, B. York FS2Crew Web Site / FS2Crew Facebook Page / FS2Crew Discord
June 6, 200917 yr Moderator Well, one way would be this simple approach...Multiply by 100 before and divide by 100 afterwards. plane_heading = floor(plane_heading*100.0 + 0.5) / 100.0 Fr. Bill AOPA Member: 07141481 AARP Member: 3209010556 Avsim Board of Directors | Avsim Forums Moderator
June 7, 200916 yr Author Commercial Member Well, one way would be this simple approach...Multiply by 100 before and divide by 100 afterwards. plane_heading = floor(plane_heading*100.0 + 0.5) / 100.0Good suggestion!Cheers, B. York FS2Crew Web Site / FS2Crew Facebook Page / FS2Crew Discord
June 7, 200916 yr Well, one way would be this simple approach...Multiply by 100 before and divide by 100 afterwards. plane_heading = floor(plane_heading*100.0 + 0.5) / 100.0What am I missing?249.0 * 100.0 = 24900.0 24900.0 + 0.5 = 24900.5 floor(24900.5) = 24900.0 24900.0 / 100 .0 = 249.0 Gerry Howard
June 8, 200916 yr Moderator More like "what did I miss..."plane_heading = (int)floor(plane_heading*100.0 + 0.5) / 100.0For it to work properly one must explicitly cast the float result to an integer... :(Or, implicitly cast in the swprintf string's format to %!3d Fr. Bill AOPA Member: 07141481 AARP Member: 3209010556 Avsim Board of Directors | Avsim Forums Moderator
June 9, 200916 yr More like "what did I miss..."plane_heading = (int)floor(plane_heading*100.0 + 0.5) / 100.0For it to work properly one must explicitly cast the float result to an integer... :( Or, implicitly cast in the swprintf string's format to %!3dI still think there's something missing.249.0 * 100.0 = 24900.0 24900.0 + 0.5 = 24900.5 int(24900.5) = 24900 24900 / 100.0 = 249.0 The following seems to give the desired answerplane_heading = int((plane_heading + 5) / 10.0) * 10.0 249.0 + 5 = 255.0 255.0 / 1- = 25.5 int(25.5) = 25.0 25.0 * 10.0 = 250.0 I think the general expression to round a number n to the nearest multiple of m is:int((n + (m / 2.0)) / n) * mE&OE Gerry Howard
June 9, 200916 yr Assuming plane_heading is a floating point variable, you can just use this:int rounded = (int)(plane_heading + 0.5);Tim Tim http://fsandm.wordpress.com
June 9, 200916 yr Assuming plane_heading is a floating point variable, you can just use this:int rounded = (int)(plane_heading + 0.5);TimThat always rounds down249.0 + 0.5 = 249.5int(249.5) = 249.0 Gerry Howard
June 9, 200916 yr Well, if your input value is 249.0, the output value should be 249, only input values equal or higher than 249.5 should round up to 250. If you want anything higher than 249.0 to be 250, then you can use (int)ceil(plane_heading).Tim Tim http://fsandm.wordpress.com
June 9, 200916 yr Maybe I'm misunderstanding, are you trying to round up to the 1's place, or the 10's place? I was assuming the 1's place, but if needing the 10's place, then the (int)((plane_heading + 5.0) / 10.0) * 10 version is correct :-> Tim Tim http://fsandm.wordpress.com
June 9, 200916 yr Moderator The 1's place, Tim.Bryan is writing a GDI+ string, and any value even slightly less than a whole number is "rounding down," e.g. 359.9 displays as 359... Fr. Bill AOPA Member: 07141481 AARP Member: 3209010556 Avsim Board of Directors | Avsim Forums Moderator
June 9, 200916 yr The 1's place, Tim.Bryan is writing a GDI+ string, and any value even slightly less than a whole number is "rounding down," e.g. 359.9 displays as 359...Then I've misunderstood. Does he want to round up or to the nearest integer? Gerry Howard
June 10, 200916 yr Moderator Then I've misunderstood. Does he want to round up or to the nearest integer?Nearest integer, whole number, or round up to whole number...As I stated, there are more than a few ways to accomplish the goal... :( Fr. Bill AOPA Member: 07141481 AARP Member: 3209010556 Avsim Board of Directors | Avsim Forums Moderator
June 10, 200916 yr Commercial Member Here's a method that actually follows the 'rule' of >= 5 for rounding: FLOAT64 input_val; // the value you wish to roundFLOAT64 frac; // the fractional value, for this example... fractional value of 1'sFLOAT64 ret_val; // the returned valuefrac = fmod(input_val,1); // extract the fractional valueret_val = input_val - frac; // remove the fractional value from the return valueif (frac>=0.5){ ret_val += 1; // if greater or equal to 0.5 add 1 to return value} Ed Wilson Mindstar AviationMy Playland - I69
June 10, 200916 yr Nearest integer, whole number, or round up to whole number...As I stated, there are more than a few ways to accomplish the goal... :(To round(!) off this discussion the following general C expressions should round a number, n, to a multiple of a number m. The number m need not be whole number - It could be, say, 2.5. Round to nearest (int)((n + (m / 2.0)) / m) * m Round up ((int)(n / m) + (fmod(n, m) > 0? 1: 0)) * m Round down (int)(n / m) * m E&OE Gerry Howard
Create an account or sign in to comment