electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Articles  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links  |  Store
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
 Forum index » DIY Hardware and Software » ChucK programming language
How do I get a greater no. of decimal places with a float?
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [8 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Sun Jun 03, 2007 9:50 am    Post subject: How do I get a greater no. of decimal places with a float? Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Sun Jun 03, 2007 11:21 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Send e-mail Visit poster's website
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Sun Jun 03, 2007 3:29 pm    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Mon Jun 04, 2007 2:37 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Send e-mail Visit poster's website
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Mon Jun 04, 2007 3:31 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Mon Jun 04, 2007 4:11 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Mon Jun 04, 2007 6:33 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Send e-mail Visit poster's website
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Mon Jun 04, 2007 3:01 pm    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [8 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
AmbiphonicDSP

Please support our site. If you click through and buy from
our affiliate partners, we earn a small commission.


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use