Old 08-14-2019, 09:42 AM   #1
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 18,022
Default Q: Get signed decimal part from number?

Given, for example, the numbers (which are themselves the result of a calculation)
-9.12, -7.45, -0.52, 0.52, 7.45, 9.12

In JS FX or lua, what is the best way to get these formatted results from them?
-12, -45, -52, +52, +45 and +12

I've tried various combos of %, floor() and ceil(), but no luck so far.

Also, do those numbers after the decimal point have a mathematical name?
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is online now   Reply With Quote
Old 08-14-2019, 10:16 AM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 7,925
Default

Quote:
Originally Posted by DarkStar View Post

Also, do those numbers after the decimal point have a mathematical name?
It's called the fractional part. Lua has the function math.modf that returns the first (integral) part and the fractional part.

Code:
local wholepart,fracpart = math.modf(-9.12)
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass, Paul(X)Stretch and λ) :
http://xenakios.wordpress.com/
Xenakios is online now   Reply With Quote
Old 08-14-2019, 10:26 AM   #3
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,247
Default

It's probably easiest to just do a string match and then cast it back to an integer.
Code:
local num = -9.12
local output = string.gsub(num, "%d+%.", "")
--> -12
If it's useful to have them separately, you can also do:
Code:
local sign, dec = string.match(num, "([^%d]?)%d+%.(%d+)
--> -, 12
Note: In either case, the output will be a string.
Lokasenna is online now   Reply With Quote
Old 08-14-2019, 11:35 PM   #4
Tale
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 2,975
Default

JSFX:

Code:
// Efficient
frac = x - (x|0);

// Might be more precise for large numbers.
frac = x >= 0 ? x - floor(x) : x - floor(x) - 1;
__________________
Martinic Kee Bass - Scanner Vibrato - Elka Panther - Tale's JSFX Pack
Tale is offline   Reply With Quote
Old 08-15-2019, 01:31 AM   #5
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 18,022
Default

Phew! Thank you guys - I can see me using each of them in different circumstances. Tale's approach is the most appropriate for my current work
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
DarkStar is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 09:47 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.