tsilenzio Posted August 17, 2007 Share Posted August 17, 2007 Example formula (I DID IT WRONG): (x + 1) - abs(x) The formula X want should output 1 when X = any positive number (including 0 UNLESS you can make the formula output 0 when X = 0)and outputs 0 when X = -1 Example of wanted output: x = -1, formula = 0 x = 0, formula = 1 x = 1, formula = 1 x = 2, formula = 1 x = 3, formula = 1 x = 4, formula = 1 so on.. OR x = 0, formula = 0 x = 1, formula = 1 x = 2, formula = 1 x = 3, formula = 1 x = 4, formula = 1 x = 5, formula = 1 so on.. If you know or think this is impossible then PLEASE let me know. Also if u cab provide an alternative that would work JUST as well! Example of alternative: x = 0, formula = 1 x = 1, formula = 2 x = 2, formula = 2 x = 3, formula = 2 x = 4, formula = 2 x = 5, formula = 2 so on.. Thank you in advance! - tsilenzio Link to comment https://forums.phpfreaks.com/topic/65376-impossible-formula/ Share on other sites More sharing options...
NArc0t1c Posted August 17, 2007 Share Posted August 17, 2007 I don't think php is able to do algebra. Anyay, from what I can see, it won't work out. Link to comment https://forums.phpfreaks.com/topic/65376-impossible-formula/#findComment-326478 Share on other sites More sharing options...
tsilenzio Posted August 17, 2007 Author Share Posted August 17, 2007 well no this is going to go in a for loop for x would be the index in the loop, but was trying to do the whole thing in one statment Link to comment https://forums.phpfreaks.com/topic/65376-impossible-formula/#findComment-326479 Share on other sites More sharing options...
Aeglos Posted August 17, 2007 Share Posted August 17, 2007 Easy, use: x/{abs(x) + 1} + 1/{abs(x) + 1} Which is the same as (x + 1)/(abs(x) + 1) Hope it helps, cheers. Link to comment https://forums.phpfreaks.com/topic/65376-impossible-formula/#findComment-326485 Share on other sites More sharing options...
gurroa Posted August 17, 2007 Share Posted August 17, 2007 function f($x) { return $x > 0 ? 1 : 0; } function f2($x) { return floor(abs($x / abs($x+0.00000001)+0.001)); } Link to comment https://forums.phpfreaks.com/topic/65376-impossible-formula/#findComment-326486 Share on other sites More sharing options...
Barand Posted August 17, 2007 Share Posted August 17, 2007 Easy, use: x/{abs(x) + 1} + 1/{abs(x) + 1} Which is the same as (x + 1)/(abs(x) + 1) Hope it helps, cheers. You need to add ceil() otherwise it doesn't work with -2, -3, ... <?php for ($x=-5; $x<=5; $x++) echo ceil(($x + 1)/(abs($x) + 1)), '<br>' ; ?> But to me, gurroa's $x = $x > 0 ? 1 : 0; seems a somewhat simpler solution Link to comment https://forums.phpfreaks.com/topic/65376-impossible-formula/#findComment-326525 Share on other sites More sharing options...
Aeglos Posted August 17, 2007 Share Posted August 17, 2007 Whoops, thanks for clarifying. Didn't test it with anything less than -1. And gurroa's alternative is indeed simpler and nicer, but I got the impression that he was looking for some math/algebraic formula (excluding boolean ). [/disclaimer] Link to comment https://forums.phpfreaks.com/topic/65376-impossible-formula/#findComment-326528 Share on other sites More sharing options...
tsilenzio Posted August 17, 2007 Author Share Posted August 17, 2007 JESUS You guys rock! I spent a couple hours on it and couldn't find an answer (I was half awake, still am) but I couldnt figure ANYTHING out so i started to think it was impossible but very few is impossible with math. I probably would have been able to do it probably if i was more awake and had taken pre-cal or calculus in high school, not that I dont love math just my counsolor didn't tell me the alternatives that i asked about that i learned later i could have really done =/ Anyways you guys dont ned to hear that stuff, thanx SOO much again guys!! MUCH MUCH appreciation Link to comment https://forums.phpfreaks.com/topic/65376-impossible-formula/#findComment-326555 Share on other sites More sharing options...
Orio Posted August 17, 2007 Share Posted August 17, 2007 You could also use: (x + abs(x)) / (2*abs(x-1) +2) Positives will give 1, Zero will give 0, Negatives will give 0. Orio. Link to comment https://forums.phpfreaks.com/topic/65376-impossible-formula/#findComment-326561 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.