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 
Live streaming at radio.electro-music.com

  host / artist show at your time
<on air> Jez Adventures in Sound
Please visit the chat
 Forum index » DIY Hardware and Software » ChucK programming language
Works with ints, but not floats.
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [3 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
Khepri



Joined: Jul 20, 2009
Posts: 18
Location: Canada

PostPosted: Sun Aug 12, 2012 5:12 pm    Post subject: Works with ints, but not floats.
Subject description: Stack overflow when using != operator
Reply with quote  Mark this post and the followings unread

Hi, all. It's been quite a while since I've looked at ChucK (not because of lack of interest.) Now that I'm back at it, a lot of things seem a bit easier to understand.

My question:
I'm wondering why this *will* work with float variables when I use '<'
(less than), but not when I use '!=' (not equal). However it -does- work fine when the variables are int, instead of float.

//---------------------------------
// Increase value of 'current' by 0.1, until it reaches 1.0


float current;

0.0 => current;
<<<current>>>;

change (1, 0.1 );

fun void change (float target, float amount)
{
if( current != target )
{
(current + amount) => current;
<<<current>>>;
change(1, 0.1);

}
}
//-----------------------------------------------

With the code above, the value of "current" does reach exactly the "target" of 1.0000, but it continues past it, to 170.7, and I get a stack overflow error message. As I said, it works as I expect it to with integers or using '<' operator { if(current < target) . . . .

_________________
Don't fear mistakes. There are none.
- Miles Davis
Back to top
View user's profile Send private message
Blue Hell
Site Admin


Joined: Apr 03, 2004
Posts: 20514
Location: The Netherlands, Enschede
Audio files: 147
G2 patch files: 318

PostPosted: Sun Aug 12, 2012 5:51 pm    Post subject: Reply with quote  Mark this post and the followings unread

Such is the nature of floats ... while 1.000000 may seem exact it is in fact a rounded representation of the actual underlying number .. the number could have been 0.9999999999999 when displayed with more digits ... when comparing floats you can not use an exact match but use some sort of "nearly equal" function.
_________________
Jan
Back to top
View user's profile Send private message Visit poster's website
Khepri



Joined: Jul 20, 2009
Posts: 18
Location: Canada

PostPosted: Sun Aug 12, 2012 6:58 pm    Post subject: Reply with quote  Mark this post and the followings unread

Ah - ok. Got it. Thanks, Jan.

Funny, how we usually tend to "believe" what a computer "says." When I see "1.000000 :(float)" come up in the console monitor, I'll be suspicious from now on Smile

_________________
Don't fear mistakes. There are none.
- Miles Davis
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 [3 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
e-m mkii

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