首页 > 解决方案 > 我正在尝试在 python ctypes 中将 float 转换为 str 但它永远不会发生

问题描述

我正在做一个简单的计算器,我需要做一些平方根函数,当我开始时它就出错了。这些是我的代码。

测试.dll

#include <stdio.h>

float negative_check(float num)
{
    if(num < 0){
        num = -num;
    }
    return num;
}

float calc_sqrt(int calcFirst)
{
    const float difference = 0.00001;
    float calcResult = 1.0;

    while(negative_check(calcResult * calcResult - calcFirst) >= difference){
        calcResult = (calcFirst/calcResult + calcResult)/2.0;
    }
    
    return calcResult;
}

Python

from ctypes import *
simple_calc = CDLL("test.dll")
result_var = simple_calc.calc_sqrt(4)
print("Result: ", result_var)

重击

Output: 4502151

但它给了我一些内存地址。我应该怎么办?

标签: pythoncctypessquaresquare-root

解决方案


推荐阅读