Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct calculation error when series type is f32 #15

Merged
merged 1 commit into from
Nov 8, 2024

Conversation

ssrtw
Copy link
Contributor

@ssrtw ssrtw commented Sep 21, 2024

Problem Cause

The Talib function expects input of type double. When the input Series has a data type of f32, it leads to abnormal calculation results. Initially, the to_float function of Polars was used for type conversion, but this function does not handle the conversion from f32 to f64 since f32 is already a floating-point type. Passing the chunk pointer of type f32 directly as double to the Talib calculation results in memory alignment issues, affecting the calculation results. As encountered in issue #11, the attached image shows that fields like adjusted_close are of type f32.
image

Changes Made

  1. Added the cast_series_to_f64 function to utils.rs, which casts non-f64 types and rechunks.
  2. Replaced the usage of to_float with cast_series_to_f64 in overlap.rs to convert non-f64 Series types, ensuring all input data for Talib functions are of type double.

let input = &mut inputs[0].to_float()?.rechunk();

let input = &mut cast_series_to_f64(&inputs[0]);
pub fn cast_series_to_f64(series: &Series) -> Series {
    match series.dtype() {
        DataType::Float64 => Ok(series.clone()),
        _ => series.cast(&DataType::Float64),
    }
    .unwrap()
    .rechunk()
}

@Yvictor Yvictor merged commit b62b5ce into Yvictor:master Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants