Skip to content

Construction using seconds is not accurate #22

@julaudo

Description

@julaudo

The folloing code

    from timecode import Timecode
    seconds = 0
    for _ in range(12):
        seconds += 0.04
        print(Timecode(framerate=25, start_seconds=seconds).frames)

prints

1
2
3
4
5
6
7
8
9
10
10
11

This is because of the float_to_tc method:

    def float_to_tc(self, seconds):
        """set the frames by using the given seconds
        """
        return int(seconds * self._int_framerate)

If the float is not exactly represented, the result may not be accurate.
In the example :
int(0.43999999999999995*25)=int(10.999999999999998)=10

It would be safer to round the result before casting to integer:

    def float_to_tc(self, seconds):
        """set the frames by using the given seconds
        """
        return int(round(seconds * self._int_framerate))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions