* forcing output type
@ 2001-09-23 15:40 Clint Adams
2001-09-23 15:47 ` Zefram
0 siblings, 1 reply; 3+ messages in thread
From: Clint Adams @ 2001-09-23 15:40 UTC (permalink / raw)
To: zsh-users
Is there a way to get either
print $(( 2/3 ))
or
float f; (( f = 2/3 )); print $f
to output the equivalent of what would be output
if, for instance, the "2" were replaced by "2.0"?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: forcing output type
2001-09-23 15:40 forcing output type Clint Adams
@ 2001-09-23 15:47 ` Zefram
2001-09-23 16:13 ` Clint Adams
0 siblings, 1 reply; 3+ messages in thread
From: Zefram @ 2001-09-23 15:47 UTC (permalink / raw)
To: Clint Adams; +Cc: zsh-users
Clint Adams wrote:
>float f; (( f = 2/3 )); print $f
>
>to output the equivalent of what would be output
>if, for instance, the "2" were replaced by "2.0"?
The expression "2/3" is a truncating integer division; its value is
exactly 0. Just as in C, if you want to do a floating-point division
you have to provide floating-point operands.
-zefram
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: forcing output type
2001-09-23 15:47 ` Zefram
@ 2001-09-23 16:13 ` Clint Adams
0 siblings, 0 replies; 3+ messages in thread
From: Clint Adams @ 2001-09-23 16:13 UTC (permalink / raw)
To: Zefram; +Cc: zsh-users
> The expression "2/3" is a truncating integer division; its value is
> exactly 0. Just as in C, if you want to do a floating-point division
> you have to provide floating-point operands.
Here's a hack that lets one do "calc 2/3" and get a floating point
answer.
calc () {
setopt localoptions extendedglob
local expr="$*"
if [[ "$expr" != *.* && "$expr" = *(#b)([0-9]##)(#B)' '#/' '#([0-9]##)* ]]
then
expr="${expr/${match[1]}/${match[1]}.0}"
fi
print $(( $expr ))
}
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2001-09-23 16:13 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-23 15:40 forcing output type Clint Adams
2001-09-23 15:47 ` Zefram
2001-09-23 16:13 ` Clint Adams
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).