[PHP] Repeating Decimals
Hello again, This is another PHP mathematical question. How can I display a bar over a number (overline) if it's a repeating decimal? When the user types in 1 by 3, they get 0.. I want it to display as 0.3 with the 3 overlined. How can I do this keeping in mind that not all numbers will be repeating decimals? Thanks, Stephen Craton http://www.melchior.us "What is a dreamer that cannot persevere?" -- http://www.melchior.us -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Repeating Decimals
> This is another PHP mathematical question. How can I display a bar over a > number (overline) if it's a repeating decimal? When the user types in 1 by > 3, they get 0.. I want it to display as 0.3 with the 3 > overlined. How can I do this keeping in mind that not all numbers will be > repeating decimals? Well, the first problem is finding a charset or something that has a number with a line over it and how you'd create that character in HTML, unless you want to use an image or something. Next, you can treat the result as a string and look at the last X digits. If they are the same, remove them all and substitute with the number and a bar. It just comes down to string manipulation... ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Repeating Decimals
0.3 Stephen wrote: Hello again, This is another PHP mathematical question. How can I display a bar over a number (overline) if it's a repeating decimal? When the user types in 1 by 3, they get 0.. I want it to display as 0.3 with the 3 overlined. How can I do this keeping in mind that not all numbers will be repeating decimals? Thanks, Stephen Craton http://www.melchior.us "What is a dreamer that cannot persevere?" -- http://www.melchior.us -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Repeating Decimals
Hi, Friday, December 6, 2002, 5:00:07 AM, you wrote: S> Hello again, S> This is another PHP mathematical question. How can I display a bar over a S> number (overline) if it's a repeating decimal? When the user types in 1 by S> 3, they get 0.. I want it to display as 0.3 with the 3 S> overlined. How can I do this keeping in mind that not all numbers will be S> repeating decimals? Did this as an exercise :) function check_to_end($str,$pat){ $lp = strlen($pat); $ls = strlen($str); $x = 0; while(true){ $ls = strlen($str); if($ls > $lp){//string bigger than pattern $t = substr($str,0,$lp); if($t != $pat){ return false; } $str = substr($str,$lp); }else{//pattern too big .. checks tail end $pat = substr($pat,0,strlen($str)); if( $pat != $str) return false; //no repeat if($x < 2) return false; //didn't repeat enough return true; //found a pattern } $x++; } } function repeat($num){ $s = substr(number_format($num,16),0,-1); //make a string echo $s; list($a,$b) = split('\.',$s); //split decimal bit out $l = strlen($b); for($i=0;$i<$l;$i++){ //loop through string $o = 0; $k = $b[$i];//number to find for($y = $i+1;$y<$l;$y++){ //loop look for same number $c = ord($b[$y]); if(ord($k) == ord($b[$y])){ //got a match $pat = substr($b,$i,$y-$i); //cut out pattern $str = substr($b,$i); //string to check if(check_to_end($str,$pat)){ //see if pattern runs till the end $o = 1; //yep if(ord($pat) == ord('0')) $o = 2; //were they just '0's break; } } } if($o)break; // all done } if($o == 1){ // a pattern $r = $a.'.'.substr($b,0,$i).''.$pat.''; }elseif($o == 2){ // whole bunch of ending 0s $r = (substr($b,0,$i) != '')? $a.'.'.substr($b,0,$i):$a; }else{ //no repeat $r = $s; } return $r; } //usage for($x = 1;$x < 100;$x++){ $a = 11/$x; $res = repeat($a); echo ' returned: '.$res.''; } The check_to_end() function could probably be replaced with a regex but those go in the same basket as vi with my old brain :) Regards Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Repeating Decimals
How would I run the functions though and then print the repeating decimal to the screen? - Original Message - From: "Tom Rogers" <[EMAIL PROTECTED]> To: "Stephen" <[EMAIL PROTECTED]> Cc: "PHP List" <[EMAIL PROTECTED]> Sent: Friday, December 06, 2002 8:38 AM Subject: Re: [PHP] Repeating Decimals > Hi, > > Friday, December 6, 2002, 5:00:07 AM, you wrote: > S> Hello again, > > S> This is another PHP mathematical question. How can I display a bar over a > S> number (overline) if it's a repeating decimal? When the user types in 1 by > S> 3, they get 0.. I want it to display as 0.3 with the 3 > S> overlined. How can I do this keeping in mind that not all numbers will be > S> repeating decimals? > > Did this as an exercise :) > > function check_to_end($str,$pat){ > $lp = strlen($pat); > $ls = strlen($str); > $x = 0; > while(true){ > $ls = strlen($str); > if($ls > $lp){//string bigger than pattern > $t = substr($str,0,$lp); > if($t != $pat){ > return false; > } > $str = substr($str,$lp); > }else{//pattern too big .. checks tail end > $pat = substr($pat,0,strlen($str)); > if( $pat != $str) return false; //no repeat > if($x < 2) return false; //didn't repeat enough > return true; //found a pattern > } > $x++; > } > } > function repeat($num){ > $s = substr(number_format($num,16),0,-1); //make a string > echo $s; > list($a,$b) = split('\.',$s); //split decimal bit out > $l = strlen($b); > for($i=0;$i<$l;$i++){ //loop through string > $o = 0; > $k = $b[$i];//number to find > for($y = $i+1;$y<$l;$y++){ //loop look for same number > $c = ord($b[$y]); > if(ord($k) == ord($b[$y])){ //got a match > $pat = substr($b,$i,$y-$i); //cut out pattern > $str = substr($b,$i); //string to check > if(check_to_end($str,$pat)){ //see if pattern runs till the end > $o = 1; //yep > if(ord($pat) == ord('0')) $o = 2; //were they just '0's > break; > } > } > } > if($o)break; // all done > } > if($o == 1){ // a pattern > $r = $a.'.'.substr($b,0,$i).''.$pat.''; > }elseif($o == 2){ // whole bunch of ending 0s > $r = (substr($b,0,$i) != '')? $a.'.'.substr($b,0,$i):$a; > }else{ //no repeat > $r = $s; > } > return $r; > } > //usage > for($x = 1;$x < 100;$x++){ > $a = 11/$x; > $res = repeat($a); > echo ' returned: '.$res.''; > } > > The check_to_end() function could probably be replaced with a regex but those > go in the same basket as vi with my old brain :) > > Regards Tom > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[2]: [PHP] Repeating Decimals
Hi, Saturday, December 7, 2002, 6:36:18 AM, you wrote: S> How would I run the functions though and then print the repeating decimal to S> the screen? The function returns the string ready to print You will need to comment out the echo $s line, it was there for debugging. just do where ever you need it -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re[4]: [PHP] Repeating Decimals
Hi, Sunday, December 8, 2002, 1:46:07 AM, you wrote: S> Ok, I'm getting some weird errors. Here's the website: S> http://check.melchior.us/module.php?id=3 S> I attached the output file. What am I doing wrong!? S> - Original Message - S> From: "Tom Rogers" <[EMAIL PROTECTED]> S> To: "Stephen" <[EMAIL PROTECTED]> S> Cc: "PHP List" <[EMAIL PROTECTED]> S> Sent: Friday, December 06, 2002 11:26 PM S> Subject: Re[2]: [PHP] Repeating Decimals >> Hi, >> >> Saturday, December 7, 2002, 6:36:18 AM, you wrote: >> S> How would I run the functions though and then print the repeating S> decimal to >> S> the screen? >> >> The function returns the string ready to print >> You will need to comment out the echo $s line, it was there for debugging. >> >> just do where ever you need it >> >> >> >> -- >> regards, >> Tom >> >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> You chopped off this line at the very begining of the function $s = substr(number_format($num,16),0,-1); //make a string -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[2]: [PHP] Repeating Decimals
Ok, I'm getting some weird errors. Here's the website: http://check.melchior.us/module.php?id=3 I attached the output file. What am I doing wrong!? - Original Message - From: "Tom Rogers" <[EMAIL PROTECTED]> To: "Stephen" <[EMAIL PROTECTED]> Cc: "PHP List" <[EMAIL PROTECTED]> Sent: Friday, December 06, 2002 11:26 PM Subject: Re[2]: [PHP] Repeating Decimals > Hi, > > Saturday, December 7, 2002, 6:36:18 AM, you wrote: > S> How would I run the functions though and then print the repeating decimal to > S> the screen? > > The function returns the string ready to print > You will need to comment out the echo $s line, it was there for debugging. > > just do where ever you need it > > > > -- > regards, > Tom > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[8]: [PHP] Repeating Decimals
That's ok. The user needs to use some of his/her brain too. :P Thanks again! - Original Message - From: "Tom Rogers" <[EMAIL PROTECTED]> To: "Stephen" <[EMAIL PROTECTED]> Sent: Saturday, December 07, 2002 2:03 PM Subject: Re[8]: [PHP] Repeating Decimals > Hi, > > Sunday, December 8, 2002, 4:51:37 AM, you wrote: > S> Thanks! I works now. > > > S> - Original Message - > S> From: "Tom Rogers" <[EMAIL PROTECTED]> > S> To: "Stephen" <[EMAIL PROTECTED]> > S> Sent: Saturday, December 07, 2002 1:46 PM > S> Subject: Re[6]: [PHP] Repeating Decimals > > Good :) > One problem is rounding which screws things up as it can round up the last > digit giving a false answer. 1/13 for example which gives 0.0769230769231 <<< 07 > rounded up to 1 > Not sure what to do about that... > > -- > regards, > Tom > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php