Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get fraction from number

309 views
Skip to first unread message

Eran....@gmail.com

unread,
Apr 27, 2006, 2:39:16 AM4/27/06
to
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.

bill...@alum.mit.edu

unread,
Apr 27, 2006, 2:50:35 AM4/27/06
to
set s 10.12345
puts 0[string range $s [string last "." $s] end]

Arjen Markus

unread,
Apr 27, 2006, 3:10:14 AM4/27/06
to
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

Bob Halpin

unread,
Apr 27, 2006, 3:35:09 AM4/27/06
to

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

suchenwi

unread,
Apr 27, 2006, 3:31:14 AM4/27/06
to
There is a function:
% expr fmod(10.325,1.)
0.325

Arjen Markus

unread,
Apr 27, 2006, 6:07:08 AM4/27/06
to
Oh, I forgot about that one!

Regards,

Arjen

Bruce Hartweg

unread,
Apr 27, 2006, 8:18:50 AM4/27/06
to
or

lindex [split $x .] 1

(but I think fmod is the way to go)

Bruce

William J Giddings

unread,
Apr 27, 2006, 10:24:28 AM4/27/06
to
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"

Donal K. Fellows

unread,
Apr 27, 2006, 10:34:37 AM4/27/06
to
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.

Donald Arseneau

unread,
Apr 27, 2006, 11:09:19 PM4/27/06
to
Eran....@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 as...@triumf.ca

Arjen Markus

unread,
May 1, 2006, 3:15:07 AM5/1/06
to
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

Donald Arseneau

unread,
May 1, 2006, 7:22:17 AM5/1/06
to
"Arjen Markus" <arjen....@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 as...@triumf.ca

Arjen Markus

unread,
May 2, 2006, 2:47:56 AM5/2/06
to
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

Les Cargill

unread,
May 2, 2006, 7:40:59 PM5/2/06
to
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...

> Regards,
>
> Arjen
>

--
Les Cargill

Les Cargill

unread,
May 2, 2006, 7:40:45 PM5/2/06
to
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*

0 new messages