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

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

- 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

Highlighted
##

I'm getting the wrong results on all box platforms.

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

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

Highlighted
##

The result of the divide is a little less than 13, which rounds down to 12 when you convert to integer. It still prints as 13 as a float since its so close to 13. The same thing happens in C:

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

--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
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

Highlighted
##

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 ????

Highlighted
##

Int always rounds down. The problem is the number you are rounding is close to but a little less than 13.

--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
09:42 AM

Re: Why does this fail ?

--Mark

Highlighted
##

Of course if you want to round to the nearest integer rather than rounding down, you can say

--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

Highlighted
##

That sometimes throughs the answer off.

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.

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.

Highlighted
##

I'm not sure you're understanding that the problem has nothing to do with the int conversion, it's due to the loss of precision in the log and divide operations. If you've ever taken a Numerical Analysis course, you may recall the many dangers of working with floating point numbers. In general, what you want to do is impossible for arbitrary real numbers; for example, 2^70 and (2^70)-1 are both represented by the same floating point number, so there's no way to tell that log2 of the first is 70 while log2 of the second is 69.

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

--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
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

Highlighted
##

Well, hm. Python has the same precision limitations, how come it works there?

EnTerr

Level 10

- 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

Highlighted
##

This appears to work for + and - floats with + and - exponents.

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.

*

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

Highlighted
##

Do you really need log2 function as such?

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

EnTerr

Level 10

- 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)