The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Newsgroups: comp.lang.tcl
From:
Eran.Ya... @gmail.com
Date: 26 Apr 2006 23:39:16 -0700
Local: Thurs, Apr 27 2006 2:39 am
Subject: Get fraction from number
Hi all, I need to get the fraction from a number. Is there a way to do it with tcl?
for example:
Number = 10.12345
I want a way to extract 0.12345 from number to other variable.
thanks for the help.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
billpo... @alum.mit.edu
Date: 26 Apr 2006 23:50:35 -0700
Local: Thurs, Apr 27 2006 2:50 am
Subject: Re: Get fraction from number
set s 10.12345 puts 0[string range $s [string last "." $s] end]
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
"Arjen Markus" <arjen.mar... @wldelft.nl>
Date: 27 Apr 2006 00:10:14 -0700
Local: Thurs, Apr 27 2006 3:10 am
Subject: Re: Get fraction from number
A method using [expr] is: set number 10.12345 set fraction [expr {$number-int($number)}]
There is no single function to do it though.
Regards,
Arjen
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
Bob Halpin <bhal... @attglobal.net>
Date: Thu, 27 Apr 2006 03:35:09 -0400
Local: Thurs, Apr 27 2006 3:35 am
Subject: Re: Get fraction from number
Eran.Ya
... @gmail.com wrote:
> Hi all,
> I need to get the fraction from a number. Is there a way to do it with > tcl?
> for example:
> Number = 10.12345
> I want a way to extract 0.12345 from number to other variable.
> thanks for the help.
Some quick hacks: % set x 10.12345 10.12345 % regexp {([0-9]*)(\.)([0-9]*)} $x match a b c 1 % set a 10 % set b . % set c 12345
or
regexp {([0-9]*)(\.[0-9]*)} $x match a b 1 % set a 10 % set b .12345
or
% expr $x-[expr int($x)] 0.12345
or . . .
Bob
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
"suchenwi" <richard.suchenwirth-bauersa... @siemens.com>
Date: 27 Apr 2006 00:31:14 -0700
Local: Thurs, Apr 27 2006 3:31 am
Subject: Re: Get fraction from number
There is a function: % expr fmod(10.325,1.) 0.325
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
"Arjen Markus" <arjen.mar... @wldelft.nl>
Date: 27 Apr 2006 03:07:08 -0700
Local: Thurs, Apr 27 2006 6:07 am
Subject: Re: Get fraction from number
Oh, I forgot about that one! Regards,
Arjen
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
Bruce Hartweg <bruce-n... @hartweg.us>
Date: Thu, 27 Apr 2006 07:18:50 -0500
Local: Thurs, Apr 27 2006 8:18 am
Subject: Re: Get fraction from number
Bob Halpin wrote:
> Eran.Ya
... @gmail.com wrote:
>> Hi all,
>> I need to get the fraction from a number. Is there a way to do it with >> tcl?
>> for example:
>> Number = 10.12345
>> I want a way to extract 0.12345 from number to other variable.
>> thanks for the help.
> Some quick hacks:
> % set x 10.12345 > 10.12345 > % regexp {([0-9]*)(\.)([0-9]*)} $x match a b c > 1 > % set a > 10 > % set b > . > % set c > 12345
> or
> regexp {([0-9]*)(\.[0-9]*)} $x match a b > 1 > % set a > 10 > % set b > .12345
> or
> % expr $x-[expr int($x)] > 0.12345
> or . . .
> Bob
or lindex [split $x .] 1
(but I think fmod is the way to go)
Bruce
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
William J Giddings <giddi... @freeuk.com>
Date: Thu, 27 Apr 2006 14:24:28 GMT
Local: Thurs, Apr 27 2006 10:24 am
Subject: Re: Get fraction from number
Remember, in Tcl everyting is a string! So, here's another way..
# frac.tcl
console show
set n 10.12345 foreach {int frac} [split $n .] {}
puts "n $n" puts "frac 0.$frac" puts "int $int"
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
"Donal K. Fellows" <donal.k.fell... @man.ac.uk>
Date: 27 Apr 2006 07:34:37 -0700
Local: Thurs, Apr 27 2006 10:34 am
Subject: Re: Get fraction from number
If the fraction isn't neat, it's sometimes useful to use [format] to clean it up after processing it with the fmod() function. % format %.3f [expr fmod(12345678901.23,1)] 0.230
Donal.
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
Donald Arseneau <a... @triumf.ca>
Date: 27 Apr 2006 20:09:19 -0700
Local: Thurs, Apr 27 2006 11:09 pm
Subject: Re: Get fraction from number
Eran.Ya
... @gmail.com writes:
> Number = 10.12345
> I want a way to extract 0.12345 from number to other variable.
Ha! That was the buggy part of a program in the cuurent thread "How to interpet Tcl/Tk stack information"! You realize that the fractional part becomes less and less meaningful as the number gets larger?
-- Donald Arseneau a... @triumf.ca
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
"Arjen Markus" <arjen.mar... @wldelft.nl>
Date: 1 May 2006 00:15:07 -0700
Local: Mon, May 1 2006 3:15 am
Subject: Re: Get fraction from number
One caveat, especially when dealing with fractions via the string representation: you need to be clear what you want from _negative_ numbers like -1.2: is the fraction 0.2 or -0.2? Regards,
Arjen
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
Donald Arseneau <a... @triumf.ca>
Date: 01 May 2006 04:22:17 -0700
Local: Mon, May 1 2006 7:22 am
Subject: Re: Get fraction from number
"Arjen Markus" <arjen.mar
... @wldelft.nl> writes:
> you need to be clear what you want from _negative_ numbers like -1.2:
> is the fraction 0.2 or -0.2?
Or 0.8? -- Donald Arseneau a... @triumf.ca
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
"Arjen Markus" <arjen.mar... @wldelft.nl>
Date: 1 May 2006 23:47:56 -0700
Local: Tues, May 2 2006 2:47 am
Subject: Re: Get fraction from number
There does not seem to be an unambiguous definition: http://mathworld.wolfram.com/FractionalPart.html (BTW, Fortran 90/95 offers two functions to compute the fraction of a number x - mod(x,1.0) and modulo(x,1.0) The first is equivalent to fmod() and returns -0.2. The other is equivalent to x - floor(x) ansd returns 0.8)
Regards,
Arjen
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
Les Cargill <lNOcarg... @cfl.Arr.com>
Date: Tue, 02 May 2006 23:40:59 GMT
Local: Tues, May 2 2006 7:40 pm
Subject: Re: Get fraction from number
Arjen Markus wrote: > There does not seem to be an unambiguous definition:
"[expr $x % $y ]" is an expression of congruence modulo, which is an equivalence relation. Therefore, by (reverse) induction, [expr 0 % n] ==> 0 implies that [expr -1 % $n] ==> [expr $n - 1] This assumes it's even defined over the negative integers.... and it's *not* defined over the reals... we're extending the concept.
http://www.maths.tcd.ie/~dwilkins/Courses/111/intro.pdf
>
http://mathworld.wolfram.com/FractionalPart.html > (BTW, Fortran 90/95 offers two functions to compute the > fraction of a number x - mod(x,1.0) and modulo(x,1.0) > The first is equivalent to fmod() and returns -0.2. The > other is equivalent to x - floor(x) ansd returns 0.8)
Kinda like [lindex [split $n . ] 1 ] ... but that's *really* not a modulo operator...
-- Les Cargill
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.tcl
From:
Les Cargill <lNOcarg... @cfl.Arr.com>
Date: Tue, 02 May 2006 23:40:45 GMT
Local: Tues, May 2 2006 7:40 pm
Subject: Re: Get fraction from number
Arjen Markus wrote: > There does not seem to be an unambiguous definition:
"[expr $x % $y ]" is an expression of congruence modulo, which is an equivalence relation. Therefore, by (reverse) induction, [expr 0 % n] ==> 0 implies that [expr -1 % $n] ==> [expr $n - 1] This assumes it's even defined over the negative integers.... and it's *not* defined over the reals... we're extending the concept.
http://www.maths.tcd.ie/~dwilkins/Courses/111/intro.pdf
>
http://mathworld.wolfram.com/FractionalPart.html > (BTW, Fortran 90/95 offers two functions to compute the > fraction of a number x - mod(x,1.0) and modulo(x,1.0) > The first is equivalent to fmod() and returns -0.2. The > other is equivalent to x - floor(x) ansd returns 0.8)
Kinda like [lindex [split $n . ] 1 ] ... but that's *really* not a modulo operator...
-- Les Cargill
You must
Sign in before you can post messages.
You do not have the permission required to post.