Old 07-14-2019, 09:48 PM   #1
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 565
Default Question about item's length and source length

Here is a simple reaper project that has only an item. I already set it to media source start and end. So the item's length and source length of it are suppose to be equal, right?

But I ran this script to check:

Code:
it=reaper.GetMediaItem(0, 0)

tk=reaper.GetActiveTake(it)

length=reaper.GetMediaItemInfo_Value(it, "D_LENGTH")

source=reaper.GetMediaItemTake_Source(tk)

orilen=reaper.GetMediaSourceLength(source)

msg("length="..length)

msg("orilen="..orilen)

msg("length-orilen="..length-orilen)
and I got this result:

Code:
length=3.5526530612245
orilen=3.5526530612245
length-orilen=4.4408920985006e-016
So why doesn't length-orilen equal to zero, but a number that is clse to zero?

Last edited by dsyrock; 04-14-2020 at 08:26 PM.
dsyrock is online now   Reply With Quote
Old 07-15-2019, 04:40 AM   #2
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

Seems there is some roubding issue, maybe from a sample or sub sample value. Not sure why.
X-Raym is offline   Reply With Quote
Old 07-19-2019, 02:52 AM   #3
juliansader
Human being with feelings
 
Join Date: Jul 2009
Posts: 3,714
Default

Check out this thread: GetMediaItemInfo_Value API does not return the real values.

* REAPER's IDE and ShowConsoleMsg don't display floating point numbers to their full precision, so numbers may seem equal even if they are not.

* 4.4408920985006e-016 is close to the limits of the previous two numbers' 64-bit float precision, so this is probably just normal floating point errors, not sample rounding.

* Floating point numbers should never be compared directly for equality. Instead, compare their difference to a tiny cutoff value such as 1e-14 (or whatever is about 14 decimals smaller than your expected value range).

Last edited by juliansader; 07-19-2019 at 03:30 AM.
juliansader is offline   Reply With Quote
Old 09-10-2021, 05:15 AM   #4
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default

Could it be, that this issue happens, because the take properties (in ticks) get converted to project time and therefore have a different precision?
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ is offline   Reply With Quote
Old 09-10-2021, 07:13 AM   #5
schwa
Administrator
 
schwa's Avatar
 
Join Date: Mar 2007
Location: NY
Posts: 15,812
Default

4e-16 is less than one-billionth of a sample at 48k
schwa is offline   Reply With Quote
Old 09-10-2021, 10:32 AM   #6
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

@schwa
This can be enough to break some equality check :P
X-Raym is offline   Reply With Quote
Old 11-03-2022, 09:59 AM   #7
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default

I found another solution for this and wanted to share it.
I'm using the following round function in order to round both values, 5 digits after the comma:

Code:
local function Round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end
Thanks to LBX for the function!
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ is offline   Reply With Quote
Old 11-04-2022, 04:31 AM   #8
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default

Okay, strike that, it works with some values, but not all.
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ is offline   Reply With Quote
Old 11-04-2022, 04:55 AM   #9
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,628
Default

The craziest thing I once did was to convert these values to string and round them character by character. Then convert back into number.
I didn't know anything about float-precision at that time o_O
__________________
Use you/she/her.Ultraschall-Api Lua Api4Reaper - Donate, if you wish

On vacation for the time being...
Meo-Ada Mespotine is offline   Reply With Quote
Old 11-04-2022, 04:55 AM   #10
_Stevie_
Human being with feelings
 
_Stevie_'s Avatar
 
Join Date: Oct 2017
Location: Black Forest
Posts: 5,066
Default

Been there, done that, got the T-Shirt...
__________________
My Reascripts forum thread | My Reascripts on GitHub
If you like or use my scripts, please support the Ukraine: Ukraine Crisis Relief Fund | DirectRelief | Save The Children | Razom
_Stevie_ is offline   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 11:10 PM.


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