Sqlite 给出了一个我无法重新创建的值

问题描述 投票:0回答:1

使用 django:这里是一些值和查询:

    max_played, avg_days_last_played = get_next_song_priority_values()

    # Calculate time since played using raw SQL
    time_since_played_expr = RawSQL("(julianday('now') - julianday(main_song.played_at))", [])

    query = Song.objects
    # Annotate priority
    songs_with_priority = query.annotate(
        time_since_played=ExpressionWrapper(time_since_played_expr, output_field=FloatField()),
        priority=(
            F('rating')
            - (F('count_played') / Value(max_played))
            + (F('time_since_played') / Value(avg_days_last_played))
        ),
    ).order_by('-priority')

我的记录:

    logger.info(f'Next Song: {next_song}')
    calculated_priority = (
            next_song.rating
            - (next_song.count_played / max_played)
            + (next_song.time_since_played / avg_days_last_played)
    )
    logger.info(f'Next Song: priority {next_song.priority:.2f} vs calc {calculated_priority:.2f}')
    logger.info(f'Next Song: rating {next_song.rating:.2f}')
    playd = next_song.count_played / max_played
    logger.info(f'Next Song: played {playd:.2f} ({next_song.count_played} / {max_played})')
    tspd = next_song.time_since_played / avg_days_last_played
    logger.info(
        f'Next Song: days {tspd:.2f} ({next_song.time_since_played} / {avg_days_last_played})'
    )

我得到:

INFO    Next Song: <Song-1489 End of the World Cold>
INFO    Next Song: priority 2.73 vs calc 2.56
INFO    Next Song: rating 0.50
INFO    Next Song: played 0.17 (1 / 6)
INFO    Next Song: days 2.23 (4.043296354357153 / 1.8125720233656466)

所以我的计算值较低。所有的值都在那里: 0.5的评级是可靠的, 如果 1 对 6 打得扎实,比赛就算数, 从结果中使用此后的时间

next_song.time_since_played

我使用的值与 sqlite 应该使用的值相同,但我的计算不同。

python django sqlite
1个回答
0
投票

在 SQLite 中,整数除法产生整数值,因此

1 / 6
=
0
:

sqlite> select 1 / 6;
0

您可以将其中一个值乘以

1.0
将其转换为浮点值,然后这应该可以工作:

sqlite> select 1 / 6.0;
0.166666666666667
    priority=(
        F('rating')
        - (F('count_played') / Value(max_played * 1.0))
        + (F('time_since_played') / Value(avg_days_last_played))
    )
© www.soinside.com 2019 - 2024. All rights reserved.