| Blockland Forums > Modification Help |
| "String always evaluates to 0" and finding a ratio |
| << < (2/3) > >> |
| Destiny/Zack0Wack0:
Well here's what I got, doesn't work very well: --- Code: ---function mHighestCommonDenominator(%x,%y) { if(%x == %y) return %x; while(%x != 0 && %y != 0) { %ox = %x; %oy = %y; %x = %oy % %ox; %y = %ox; echo(%ox SPC %oy); } return %x == 0 ? %y : %x; } function getRatio(%width,%height) { %ratio = %width / %height; %lhs = getSubStr(%ratio,0,7); //get the first 5 digits, ie. 0.***** %lhs *= 100; %rhs = getSubStr(%ratio,0,5); //first 3 digits %rational = %lhs - %rhs; //%rational / 99 now == %ratio %denominator = mHighestCommonDenominator(mFloatLength(%rational,0),99); return mFloatLength(%rational / %denominator,0) @ ":" @ 99 / %denominator; //works for widescreens if I do return mFloatLength(%rational / 11,0) @ ":" @ 9; } --- End code --- Also, so you know, getRes is already defined by the game. |
| Chrono:
--- Quote from: Nexus on July 06, 2011, 07:22:31 PM ---What if an integer troll makes the game divide by zero? --- End quote --- It'll return 0:0. |
| Greek2me:
--- Quote from: Chrono on July 07, 2011, 04:32:14 PM ---It'll return 0:0. --- End quote --- But wouldn't 2 / (mCeil(0) - 0) be 2 / 0, which would crash it. |
| Chrono:
--- Quote from: Greek2me on July 07, 2011, 05:14:37 PM ---But wouldn't 2 / (mCeil(0) - 0) be 2 / 0, which would crash it. --- End quote --- Type echo(2/0); in your console and see if it crashes you. |
| Crysist:
Oh I already solved this, yet the function I made checks every denominator and sees if it gets a fraction 0.000001 from the number you enter. --- Code: ---function getratio(%ratio,%i) { if(%i $="") { %i=1; } else if(%i<99999) { %raat = %ratio*%i; %rate = mCeil(%raat - 0.5); %diff = %raat - %rate; if(%diff <= 0.000001 && %diff >= -0.000001) { %raat = mCeil(%raat - 0.5); echo(%raat@":"@%i); return %raat@":"@%i; } else { getratio(%ratio,%i++); } } else { echo("Denominator is greater than 100000"); echo(%ratio@":1"); return %ratio@":1"; } } --- End code --- |
| Navigation |
| Message Index |
| Next page |
| Previous page |