Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
belltown
Roku Guru

Double Trouble

1. How do I store a Double (or roDouble) in an roAssociativeArray or roArray?

All my attempts to store a Double in an roAssociativeArray or roArray result in an roFloat being stored instead.

2. How do I define a Double literal with an exponent?

The use of 'D' to define the exponent for a Double literal seems to be ignored completely (using print, or the debugger 'var' command):
E.g. 123E36 prints as 1.23e+38 (correct), whereas 123D36 prints as 123 (wrong - ignores the exponent)
E.g. 12345678D40 prints as 12345678 (ignores the exponent), and var shows: val:1.23457e+07


BrightScript Debugger> ------ Running ------

123456789012 roFloat
2345678901234 roFloat
12345 roFloat
1234.567 roFloat
123.999 roFloat
1.23456789 roFloat

123456789012 roFloat
2345678901234 roFloat
12345 roFloat
1234.567 roFloat
123.999 roFloat
1.23456789 roFloat
BrightScript Micro Debugger.
Enter any BrightScript statement, debug commands, or HELP.

Current Function:
001: sub main ()
002: aa = CreateObject ("roAssociativeArray")
003: dbl1 = 123456789012
004: dbl2 = 2345678901234D40
005: dbl3 = 12345#
006: dbl4# = 1234.567D-50
007: dbl5 = CreateObject ("roDouble")
008: dbl5 = 123.999#
009: dbl6 = 1.23456789D-12
010: aa.dbl1 = dbl1
011: aa.dbl2 = dbl2
012: aa.dbl3 = dbl3
013: aa.dbl4 = dbl4#
014: aa.dbl5 = dbl5
015: aa.dbl6 = dbl6
016: ?
017: ?aa.dbl1, type (aa.dbl1)
018: ?aa.dbl2, type (aa.dbl2)
019: ?aa.dbl3, type (aa.dbl3)
020: ?aa.dbl4, type (aa.dbl4)
021: ?aa.dbl5, type (aa.dbl5)
022: ?aa.dbl6, type (aa.dbl6)
023: ?
024: bb = CreateObject ("roArray", 5, true)
025: bb.Push (dbl1)
026: bb.Push (dbl2)
027: bb.Push (dbl3)
028: bb.Push (dbl4#)
029: bb.Push (dbl5)
030: bb.Push (dbl6)
031: ?bb[0], type (bb[0])
032: ?bb[1], type (bb[1])
033: ?bb[2], type (bb[2])
034: ?bb[3], type (bb[3])
035: ?bb[4], type (bb[4])
036: ?bb[5], type (bb[5])
037: stop
038: end sub
STOP (runtime error &hf7) in ...SAXSI2/pkg:/source/fptest.brs(37)
037: stop
Backtrace:
Function main() As Void
file/line: /tmp/plugin/GACAAASAXSI2/pkg:/source/fptest.brs(38)

Local Variables:
global &h0020 rotINTERFACE:ifGlobal
m &h0010 bsc:roAssociativeArray, refcnt=2
aa &h0010 bsc:roAssociativeArray, refcnt=1
dbl1 &h0010 Double val:1.23457e+11, refcnt=3
dbl2 &h0010 Double val:2.34568e+12, refcnt=3
dbl3 &h0010 Double val:12345, refcnt=3
dbl4# &h0010 Double val:1234.57, refcnt=3
dbl5 &h0010 Double val:123.999, refcnt=3
dbl6 &h0010 Double val:1.23457, refcnt=3
bb &h0010 bsc:roArray, refcnt=1
BrightScript Debugger>


I get the same results using either the 3.1 or 5.5 firmware.
0 Kudos
5 REPLIES 5
dcrandall
Visitor

Re: Double Trouble

Does the 'box' function help you, at all?
0 Kudos
belltown
Roku Guru

Re: Double Trouble

"dcrandall" wrote:
Does the 'box' function help you, at all?

Nope. Same result ...


BrightScript Debugger> ------ Running ------

123456789012 roFloat
2345678901234 roFloat
12345 roFloat
1234.567 roFloat
123.999 roFloat
1.23456789 roFloat

123456789012 roFloat
2345678901234 roFloat
12345 roFloat
1234.567 roFloat
123.999 roFloat
1.23456789 roFloat
BrightScript Micro Debugger.
Enter any BrightScript statement, debug commands, or HELP.

Current Function:
001: sub main ()
002: aa = CreateObject ("roAssociativeArray")
003: dbl1 = 123456789012
004: dbl2 = 2345678901234D40
005: dbl3 = 12345#
006: dbl4# = 1234.567D-50
007: dbl5 = CreateObject ("roDouble")
008: dbl5 = 123.999#
009: dbl6 = 1.23456789D-12
010: aa.dbl1 = Box (dbl1)
011: aa.dbl2 = Box (dbl2)
012: aa.dbl3 = Box (dbl3)
013: aa.dbl4 = Box (dbl4#)
014: aa.dbl5 = Box (dbl5)
015: aa.dbl6 = Box (dbl6)
016: ?
017: ?aa.dbl1, type (aa.dbl1)
018: ?aa.dbl2, type (aa.dbl2)
019: ?aa.dbl3, type (aa.dbl3)
020: ?aa.dbl4, type (aa.dbl4)
021: ?aa.dbl5, type (aa.dbl5)
022: ?aa.dbl6, type (aa.dbl6)
023: ?
024: bb = CreateObject ("roArray", 5, true)
025: bb.Push (Box (dbl1))
026: bb.Push (Box (dbl2))
027: bb.Push (Box (dbl3))
028: bb.Push (Box (dbl4#))
029: bb.Push (Box (dbl5))
030: bb.Push (Box (dbl6))
031: ?bb[0], type (bb[0])
032: ?bb[1], type (bb[1])
033: ?bb[2], type (bb[2])
034: ?bb[3], type (bb[3])
035: ?bb[4], type (bb[4])
036: ?bb[5], type (bb[5])
037: stop
038: end sub
STOP (runtime error &hf7) in ...MzasL4/pkg:/source/fptest.brs(37)
037: stop
Backtrace:
Function main() As Void
file/line: /tmp/plugin/PBCAAAMzasL4/pkg:/source/fptest.brs(38)

Local Variables:
global &h0020 rotINTERFACE:ifGlobal
m &h0010 bsc:roAssociativeArray, refcnt=2
aa &h0010 bsc:roAssociativeArray, refcnt=1
dbl1 &h0010 Double val:1.23457e+11, refcnt=3
dbl2 &h0010 Double val:2.34568e+12, refcnt=3
dbl3 &h0010 Double val:12345, refcnt=3
dbl4# &h0010 Double val:1234.57, refcnt=3
dbl5 &h0010 Double val:123.999, refcnt=3
dbl6 &h0010 Double val:1.23457, refcnt=3
bb &h0010 bsc:roArray, refcnt=1
BrightScript Debugger>
0 Kudos
destruk
Binge Watcher

Re: Double Trouble

Can you store a double as a string and manually convert it?
0 Kudos
belltown
Roku Guru

Re: Double Trouble

"destruk" wrote:
Can you store a double as a string and manually convert it?

That sounds like a pain in the butt, especially considering the BrightScript language is supposed to support double-precision data types.

I just discovered that I can store a Double in an roAssociativeArray or roArray by creating an array element of (undocumented) type "roDouble" inside the array itself, then calling the ifDouble interface method, SetDouble (), on that element. Any attempt, however, to assign an object that's already a Double or roDouble to an array or associative array results in the item being stored as an roFloat.


BrightScript Debugger> ------ Running ------

123456789012 roDouble
2345678901234 roDouble
12345 roDouble
1234.567 roDouble
123.999 roDouble
1.23456789 roDouble

123456789012 roDouble
2345678901234 roDouble
2345678901234 roDouble
1234.567 roDouble
123.999 roDouble
1.23456789 roDouble
BrightScript Micro Debugger.
Enter any BrightScript statement, debug commands, or HELP.

Current Function:
001: sub main ()
002: aa = CreateObject ("roAssociativeArray")
003: dbl1 = 123456789012
004: dbl2 = 2345678901234D40
005: dbl3 = 12345#
006: dbl4# = 1234.567D-50
007: dbl5 = CreateObject ("roDouble")
008: dbl5 = 123.999#
009: dbl6 = 1.23456789D-12
010: aa.dbl1 = CreateObject ("roDouble"): aa.dbl1.SetDouble (dbl1)
011: aa.dbl2 = CreateObject ("roDouble"): aa.dbl2.SetDouble (dbl2)
012: aa.dbl3 = CreateObject ("roDouble"): aa.dbl3.SetDouble (dbl3)
013: aa.dbl4 = CreateObject ("roDouble"): aa.dbl4.SetDouble (dbl4#)
014: aa.dbl5 = CreateObject ("roDouble"): aa.dbl5.SetDouble (dbl5)
015: aa.dbl6 = CreateObject ("roDouble"): aa.dbl6.SetDouble (dbl6)
016: ?
017: ?aa.dbl1, type (aa.dbl1)
018: ?aa.dbl2, type (aa.dbl2)
019: ?aa.dbl3, type (aa.dbl3)
020: ?aa.dbl4, type (aa.dbl4)
021: ?aa.dbl5, type (aa.dbl5)
022: ?aa.dbl6, type (aa.dbl6)
023: ?
024: bb = CreateObject ("roArray", 5, true)
025: bb[0] = CreateObject ("roDouble"): bb[0].SetDouble (dbl1)
026: bb[1] = CreateObject ("roDouble"): bb[1].SetDouble (dbl2)
027: bb[2] = CreateObject ("roDouble"): bb[2].SetDouble (dbl2)
028: bb[3] = CreateObject ("roDouble"): bb[3].SetDouble (dbl4#)
029: bb[4] = CreateObject ("roDouble"): bb[4].SetDouble (dbl5)
030: bb[5] = CreateObject ("roDouble"): bb[5].SetDouble (dbl6)
031: ?bb[0], type (bb[0])
032: ?bb[1], type (bb[1])
033: ?bb[2], type (bb[2])
034: ?bb[3], type (bb[3])
035: ?bb[4], type (bb[4])
036: ?bb[5], type (bb[5])
037: stop
038: end sub
STOP (runtime error &hf7) in ...IbqHmM/pkg:/source/fptest.brs(37)
037: stop
Backtrace:
Function main() As Void
file/line: /tmp/plugin/BDCAAAIbqHmM/pkg:/source/fptest.brs(38)

Local Variables:
global &h0020 rotINTERFACE:ifGlobal
m &h0010 bsc:roAssociativeArray, refcnt=2
aa &h0010 bsc:roAssociativeArray, refcnt=1
dbl1 &h0010 Double val:1.23457e+11, refcnt=1
dbl2 &h0010 Double val:2.34568e+12, refcnt=1
dbl3 &h0010 Double val:12345, refcnt=1
dbl4# &h0010 Double val:1234.57, refcnt=1
dbl5 &h0010 Double val:123.999, refcnt=1
dbl6 &h0010 Double val:1.23457, refcnt=1
bb &h0010 bsc:roArray, refcnt=1
BrightScript Debugger>


I still have yet to get an explanation as to why the "D" exponents don't work as documented for Double data types. It seems that the "D" character is recognized and the number stored as a Double rather than a Float, but the value of the exponent is ignored. Is this a bug?
0 Kudos
EnTerr
Roku Guru

Re: Double Trouble

"belltown" wrote:
1. How do I store a Double (or roDouble) in an roAssociativeArray or roArray? All my attempts to store a Double in an roAssociativeArray or roArray result in an roFloat being stored instead.
2. How do I define a Double literal with an exponent?

BSD> ? 1e40
inf 'this is a Float, it maxes at 1e38
BSD> ? 1e40#
1e+40 'this is a Double, no trouble
BSD> a = [ 1e40# ]
BSD> ? type(a[0])
roFloat 'wat!
BSD> ? a
1e+40 'well this is odd - it held the value fine. Are you sure it is a float or were you lying to me?
BSD> ? type(a[0], 3)
Double '*crickets*


"Kozma Prutkov" wrote:
"If you see a "buffalo" sign on an elephant's cage, do not trust your eyes."
0 Kudos