Pages: [1]
  Print  
Author Topic: screw angle()  (Read 1347 times)
Offline (Male) RetroX
Posted on: July 24, 2010, 01:21:49 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Code: [Select]
inline int makerange(double value, double min, double max) { return fmod(value,abs(max-min))+std::min(min,max); }
How can I make this work for negative numbers without using while? :(
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Female) IsmAvatar
Reply #1 Posted on: July 24, 2010, 05:59:56 PM

LateralGM Developer
LGM Developer
Location: Pennsylvania/USA
Joined: Apr 2008
Posts: 891

View Profile Email
fmod can be used to bring a negative number into the negative range. After that, you simply add the range to mod it into positive if desired.

Pardon the handwaving, I haven't worked the math out in my head yet.
Logged
Offline (Male) RetroX
Reply #2 Posted on: July 24, 2010, 06:50:03 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
So, in other words, add the range on after performing that calculation if it's negative?

That won't work. :/
« Last Edit: July 24, 2010, 06:53:07 PM by RetroX » Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Female) IsmAvatar
Reply #3 Posted on: July 24, 2010, 10:38:28 PM

LateralGM Developer
LGM Developer
Location: Pennsylvania/USA
Joined: Apr 2008
Posts: 891

View Profile Email
It might help if we knew exactly what the function was supposed to do and how it is expected to behave on negative numbers.
Logged
Offline (Male) RetroX
Reply #4 Posted on: July 25, 2010, 07:42:55 AM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
It fits a number within a range.

makerange(-45,0,360) should return 315, but it doesn't.

Also, I've fixed this:
Code: [Select]
inline double makerange(double value, double min, double max) { return fmod(value,abs(max-min))+(value<0 ? std::max(min,max) : std::min(min,max)); }
« Last Edit: July 25, 2010, 07:52:51 AM by RetroX » Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Female) IsmAvatar
Reply #5 Posted on: July 25, 2010, 12:41:22 PM

LateralGM Developer
LGM Developer
Location: Pennsylvania/USA
Joined: Apr 2008
Posts: 891

View Profile Email
Quote
It fits a number within a range.
Code: [Select]
if (value < min) return min; if (value > max) return max; return value;
Code: [Select]
return value < min ? min : (value > max ? max : value);
« Last Edit: July 25, 2010, 12:47:54 PM by IsmAvatar » Logged
Offline (Unknown gender) luiscubal
Reply #6 Posted on: July 25, 2010, 04:48:59 PM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
I believe RetroX wants to fit a number within a range but using a specific algorithm.
The fact he used "angle" in the title of the question suggests that when using makerange with min=0 and max=360, it should return the same angle(in degrees) but within that range.

In other words, I believe RetroX is looking for an efficient way to write this:
Code: [Select]
while(value < min)
 value += (max - min);
while (value >= max)
 value -= (max - min);

Although the >= in the max doesn't seem to perfectly match what he said, so I guess I'll have to wait for his feedback.
Logged
Offline (Male) RetroX
Reply #7 Posted on: July 25, 2010, 05:19:21 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Yeah, and here's how I fixed it:
Code: [Select]
fmod(value,fabs(max-min))+(value<0 ? std::max(min,max) : std::min(min,max))
It seems to work.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Pages: [1]
  Print