Author |
Message |
Dr. Spankenstein
Joined: Mar 03, 2007 Posts: 136 Location: Cambridge
Audio files: 1
|
Posted: Sun Jun 03, 2007 9:50 am Post subject:
How do I get a greater no. of decimal places with a float? |
 |
|
Hi guys.
This might be obvious to some but how do I get a greater number of decimal places for a floating point value because at the moment ChucK only gives me 6 decimal places?!
I.e.
0.123456 (what I'm getting)
0.123456789 (what I'd like)
Thanks
Rhys |
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Sun Jun 03, 2007 11:21 am Post subject:
|
 |
|
I believe that ChucK internally uses double-floats so that's in fact a very high resolution internally. If you'd like to print more of it you could try something like this;
<<<my_float, (my_float * 1000000)%1>>>;
I'm fairly certain that should work but I just thought it up myself.... _________________ Kassen |
|
Back to top
|
|
 |
Dr. Spankenstein
Joined: Mar 03, 2007 Posts: 136 Location: Cambridge
Audio files: 1
|
Posted: Sun Jun 03, 2007 3:29 pm Post subject:
|
 |
|
Thanks Kassen.
Another noodle scratcher for you...
Why cant I change an array inside an if statement?
Code: |
[1,1,2,3,5,8] @=> int foo[];
<<<foo[0]>>>;
|
Prints element 0 which is 1 no problem
However....
Code: |
if (true)
{
[1,1,2,3,5,8] @=> int foo[];
}
<<<foo[0]>>>;
|
Wont work the same way....why?
I'd like to do it this way so I can change the elements in an array of varying length depending on the outcome of a "random" number.
Thanks
Rhys |
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Mon Jun 04, 2007 2:37 am Post subject:
|
 |
|
Dr. Spankenstein wrote: |
Why cant I change an array inside an if statement?
|
You can. What you are doing here is in fact *defining* a array inside of a if statement, that's quite different.
What you could do instead, to get varying length arrays, would be defining a very long array and using the asociative part to define what the last element you care about is. You'd then use foo["last_element_as_defined_by_me"] instead of foo.cap(). The downside is that you'd have to know in advance how long is long enough. _________________ Kassen |
|
Back to top
|
|
 |
Dr. Spankenstein
Joined: Mar 03, 2007 Posts: 136 Location: Cambridge
Audio files: 1
|
Posted: Mon Jun 04, 2007 3:31 am Post subject:
|
 |
|
So if I had the elements...
[0.5, 1.0, 1.0, 1.0, 0.5]
and I wanted it to go into (in order)...
float foo[]
I would have to type....
Code: |
float foo[100];
if (true)
{
0.5 => foo[0];
1.0 => foo[1];
1.0 => foo[2]; //etc
}
|
Isnt this a bit long winded?
IGNORE all of the above...I was being stupid and have now figured out where I was going wrong! What I wanted to type was this all along:
Code: |
float foo[];
if (true)
{
[0.5, 1.0, 1.0, 1.0, 0.5] @=> foo;
}
|
Now I've just got to get my head around multi-dimensional arrays.
Thanks again Kassen
Rhys |
|
Back to top
|
|
 |
Dr. Spankenstein
Joined: Mar 03, 2007 Posts: 136 Location: Cambridge
Audio files: 1
|
Posted: Mon Jun 04, 2007 4:11 am Post subject:
|
 |
|
Ok so I have a multi-dimensional array and want to acess the value at the first element but ignore the first [] value and only take the second [] value how would I go about doing that.
I.e
I have:
Code: |
int foo [][];
[12,0] @=> foo;
|
How do I acesss the first element value 0 (in the second dimension?) and completely ignore the first element value 12 that goes with it? Or is there a better way to do this....? |
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Mon Jun 04, 2007 6:33 am Post subject:
|
 |
|
Basically, you should look at 2d arrays as a paper for math with those squares printed on it.
Let's say we have a 2d array of 3x3 size;
int matrix[3][3];
Suppose we wanted it to look like this;
0 0 0
0 1 3
0 0 0
In that case we'd do this;
int matrix[3][3];
1 => matrix[1][1];
3 => matrix[1][2];
This is asuming we start numbering at the upper left and considder the vertical dimention the first number. There are more examples in the chapter of the manual on arrays, it's not that hard but it does get harder to mentally picture it once you get into larger dimentions. _________________ Kassen |
|
Back to top
|
|
 |
Dr. Spankenstein
Joined: Mar 03, 2007 Posts: 136 Location: Cambridge
Audio files: 1
|
Posted: Mon Jun 04, 2007 3:01 pm Post subject:
|
 |
|
Aye I had a look at the manual before posting and was none the wiser.
Thankyou for painting a clearer picture for me though, I finally understand thanks to the visual repesentation!
Rhys |
|
Back to top
|
|
 |
|