awk's mod operator % question (application: log base2 non-integer result) 
Author Message
 awk's mod operator % question (application: log base2 non-integer result)

All:
 Non-inituitive query

citing table 2-8 in the The Awk Language book

 x % y  remainder of x divided by y

Does y need to be an integer?

I'm asking as I'm trying to evaluate log base 2 of a positive integer x
and trying to determine what the non-integer portion of the following:

 log(x) / log(2)

and round up log(x) / log(2) to the next integer
if log(x) % log(2) is > 0.

[If it's more optimal to convert x to binary, convert it to string,
 count the number of places bast the leftmost one to get
 the integer portion of the log base 2 of x, and bitwise or
 those rightmost places to detect a round-up, please post a
 solution/alternative...]

regards,
Jim Mrowca



Fri, 03 Jan 2003 03:00:00 GMT  
 awk's mod operator % question (application: log base2 non-integer result)


<snip>

Quote:
>x % y  remainder of x divided by y

>Does y need to be an integer?

A little experimentation goes a long way:

% awk 'BEGIN { print 2.7 % 1.5; exit }'
1.2

I get the same answer using MKS Awk (Toolkit 6.2), gawk 3.0.4, and
BWK's awk95.

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 03 Jan 2003 03:00:00 GMT  
 awk's mod operator % question (application: log base2 non-integer result)


Quote:
>All:
> Non-inituitive query

>citing table 2-8 in the The Awk Language book

> x % y  remainder of x divided by y

>Does y need to be an integer?

No.  it returns the same thing as fmod, according to my man page. YMMV

BTW, why didn't you just test it?

echo 10.1 3.1 | awk '{print $1 % $2}'

produces

0.8

Chuck Demas
Needham, Mass.

Quote:

>I'm asking as I'm trying to evaluate log base 2 of a positive integer x
>and trying to determine what the non-integer portion of the following:

> log(x) / log(2)

>and round up log(x) / log(2) to the next integer
>if log(x) % log(2) is > 0.

>[If it's more optimal to convert x to binary, convert it to string,
> count the number of places bast the leftmost one to get
> the integer portion of the log base 2 of x, and bitwise or
> those rightmost places to detect a round-up, please post a
> solution/alternative...]

>regards,
>Jim Mrowca

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Fri, 03 Jan 2003 03:00:00 GMT  
 awk's mod operator % question (application: log base2 non-integer result)


%  x % y  remainder of x divided by y
%
% Does y need to be an integer?

According to the single unix specification, % is equivalent to the ANSI C
function fmod(). My local man page for fmod() reads
 The fmod() and the fmodf() functions return the value x-i*y, for some in-
 teger i such that, if y is non-zero, the result has the same sign as x
 and magnitude less than the magnitude of y
--

Patrick TJ McPhee
East York  Canada



Sat, 04 Jan 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Convert result of relational operator to non-scalar value

2. Pep 238 Why new // operator (Non-integer Division)

3. why awk don't display the results?

4. beginners:Synthesability of mod operator,and division query

5. Mod operator

6. MOD operator

7. Mod operator in Eclipse

8. CARDINAL and INTEGER number in Mod-2

9. mod from 2 64bit Integer

10. Question: CPO's & Fixed Point Operator

11. Humble Beginner's Question: How do I mul 2 dwords and display results

12. 'library' of .mod files

 

 
Powered by phpBB® Forum Software