Developers and content creators—a complete solution for growing an audience directly.

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Roku Community
- :
- Developers
- :
- Roku Developer Program
- :
- Why does this fail ?

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

greubel

Level 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-25-2014
07:34 AM

Why does this fail ?

x = Log(8192.0)/Log(2)

? "x = " Type(x) " " x

y = Int( x )

? "y = " Type(y) " " y

Int does round but on a whole number and down ?

x = Float 13

y = Integer 12

16 Replies

RokuMarkn

Level 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-25-2014
09:31 AM

Re: Why does this fail ?

float a = log(8192.0);

float b = log(2.0);

float x = a / b;

int y = x;

printf("%g %d\n", x, y);

If you printf x in %A format, it's 0X1.9FFFFEP+3 while a real 13 is 0X1.AP+3.

--Mark

greubel

Level 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-25-2014
09:41 AM

Re: Why does this fail ?

So we never know which way int() is going to round ?

In this case, it should have been up ????

In this case, it should have been up ????

RokuMarkn

Level 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-25-2014
09:42 AM

Re: Why does this fail ?

--Mark

RokuMarkn

Level 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-25-2014
10:17 AM

Re: Why does this fail ?

y = Int(x + 0.5)

--Mark

greubel

Level 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-25-2014
11:36 AM

Re: Why does this fail ?

I though that fix(x) was supposed to do that, but it doesn't work either.

There should be an exact way to do it. Either round down or round up or just chop it off.

What I'm looking for is the exponent (power of 2) of a real number as an integer.

RokuMarkn

Level 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-25-2014
12:20 PM

Re: Why does this fail ?

I would probably use something like this. It's rather ugly (and untested) but it should be more accurate than using logs.

function Log2(x as Double) as Integer

lg = 0

val = 1.0

while x > val

lg = lg + 1

val = val * 2

end while

return lg

end function

--Mark

EnTerr

Level 8

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-25-2014
02:53 PM

Re: Why does this fail ?

>>> from math import log

>>> 0.1 + 0.2

0.30000000000000004

>>> 2.**70, 2.**70 - 1

(1.1805916207174113e+21, 1.1805916207174113e+21)

>>> 2.**70 == 2.**70 - 1

True

>>> log(8192) / log(2)

13.0

>>> log(8192) / log(2) == 13

True

greubel

Level 7

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-25-2014
03:21 PM

Re: Why does this fail ?

What I'm trying to do is to pack / unpack a "binary" real number from / to a roByteArray.

The float is done, at least it's been running for an hour, feeding the routines random values.

Now for the double.

*

function Log2(x as object) as Integer

if Sgn(x) < 0

x = x * -1.0

end if

lg = 0

val = 1.0

if val < x

while x > val

lg = lg + 1

val = val * 2

end while

lg = lg -1

elseif val > x

while x < val

lg = lg - 1

val = val / 2

end while

end if

return lg

end function

EnTerr

Level 8

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-25-2014
04:20 PM

Re: Why does this fail ?

I am asking because you are already building the mantissa (significand?) while calculating the exponent part. Intuitively feels to me the process is:

sign = sgn(x)

x = abs(x)

exp = 0

while x >= 2: x = x / 2: exp = exp - 1: end while

while x < 1: x = x * 2: exp = exp + 1 : end while

mant = int((x - 1) * 2^23 + 0.5)