- Counting digits in the fractional part of a number
- 17 Apr 2015 12:53:10 pm
- Last edited by M. I. Wright on 17 Apr 2015 01:35:35 pm; edited 1 time in total
Say you have a number like 5.03; what would be the best way to determine the total number of digits in that?
It could be done with a while loop, like this:
Code:
But Tari mentioned in #cemetech that it could be done with logarithms. Any ideas? This is the IRC discussion
It could be done with a while loop, like this:
Code:
//N=5.03
While fPart(N
10N->N
End
//N=503
1+int(log(N->N
//N=3
But Tari mentioned in #cemetech that it could be done with logarithms. Any ideas? This is the IRC discussion
SAX/IRC wrote:
10:37:25 (C) KermMartian: 1+int(log(iPart(N)))...?
10:37:48 (C) KermMartian: Or do you need the number of digits after the decimal point too?
10:38:30 (C) M. I. Wright: Yeah, those are important (hence the while loop that I mentioned earlier; would that be the fastest way to do it?)
10:39:05 (#) Tari: you can do it with logarithms
10:39:21 (#) Tari: something along the lines of log10(n)^-1
10:39:31 (#) Tari: and probably a ceiling in there somewhere
10:40:25 (#) Tari: log10(fPart(n)) that is
10:40:37 (C) M. I. Wright: huh. Haven't gotten to logarithms in math yet (although I understand what they do, I don't know how to use them)
10:40:39 (#) Tari: for the number of digits in the fractional part of n
10:41:36 (C) KermMartian: Wouldn't that only give you the position of the first nonzero digit in the fractional part?
10:41:49 (C) KermMartian: Rather than the position of the last digit, which is more what he wants?
10:41:58 (C) KermMartian: fPart(0.123) gives you .123, not 123.
10:42:35 (#) Tari: simple example which I'm working with, say 1.11
10:42:36 (C) M. I. Wright: If you raise it to the power -1 I think it works?
10:42:50 (#) Tari: log10(0.11)^-1 = -1.04
10:43:08 (#) Tari: ceiling(-1 * -1.04) = 2 digits
10:43:23 (#) calc84: wouldn't it be about the same for 1.1111111 though?
10:43:33 (C) KermMartian: Yes
10:43:34 (C) KermMartian: I think so too
10:43:39 (#) Tari: yeah, that's no good
10:44:00 (C) KermMartian: ~give M. I. Wright topic
10:37:48 (C) KermMartian: Or do you need the number of digits after the decimal point too?
10:38:30 (C) M. I. Wright: Yeah, those are important (hence the while loop that I mentioned earlier; would that be the fastest way to do it?)
10:39:05 (#) Tari: you can do it with logarithms
10:39:21 (#) Tari: something along the lines of log10(n)^-1
10:39:31 (#) Tari: and probably a ceiling in there somewhere
10:40:25 (#) Tari: log10(fPart(n)) that is
10:40:37 (C) M. I. Wright: huh. Haven't gotten to logarithms in math yet (although I understand what they do, I don't know how to use them)
10:40:39 (#) Tari: for the number of digits in the fractional part of n
10:41:36 (C) KermMartian: Wouldn't that only give you the position of the first nonzero digit in the fractional part?
10:41:49 (C) KermMartian: Rather than the position of the last digit, which is more what he wants?
10:41:58 (C) KermMartian: fPart(0.123) gives you .123, not 123.
10:42:35 (#) Tari: simple example which I'm working with, say 1.11
10:42:36 (C) M. I. Wright: If you raise it to the power -1 I think it works?
10:42:50 (#) Tari: log10(0.11)^-1 = -1.04
10:43:08 (#) Tari: ceiling(-1 * -1.04) = 2 digits
10:43:23 (#) calc84: wouldn't it be about the same for 1.1111111 though?
10:43:33 (C) KermMartian: Yes
10:43:34 (C) KermMartian: I think so too
10:43:39 (#) Tari: yeah, that's no good
10:44:00 (C) KermMartian: ~give M. I. Wright topic