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.
set number 10.12345
set fraction [expr {$number-int($number)}]
There is no single function to do it though.
Regards,
Arjen
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
Regards,
Arjen
lindex [split $x .] 1
(but I think fmod is the way to go)
Bruce
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"
% format %.3f [expr fmod(12345678901.23,1)]
0.230
Donal.
> 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
Regards,
Arjen
> 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
(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
> 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
> 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*