[Python] Restrict length of float in NumPy

Discussão em 'Python' iniciado por Stack, Julho 13, 2025.

  1. Stack

    Stack Membro Participativo

    I am dealing with numpy arrays and am running into an issue. It was giving a runtime-warning that I am trying to divide by zero. On further inspection I found this Error:

    Traceback (most recent call last):
    File "C:\Users\Me\Desktop\pyproj\ailib\main.py", line 36, in <module>
    model_rating.append(sum(abs((mai.calculate_model(input_array,models[x]))-answer_array)))
    File "C:\Users\Me\Desktop\pyproj\ailib\myailib.py", line 102, in calculate_model
    output_arr = calculate_layer(input_arr2,layer,connectors)
    File "C:\Users\Me\Desktop\pyproj\ailib\myailib.py", line 93, in calculate_layer
    output_arr = node * (input_arr[con1]/input_arr[con2])
    FloatingPointError: underflow encountered in scalar divide


    After further digging I found that this was happening because it was attemting to divide these two numbers: 8.884313185203669e-161 and 1.5399031202408477e+160. I am guessing that numpy rounds them to zero, and hence the divide by zero. Is there a way to fix this, like by restricting the number of digits?

    My program slowly edits the NumPy array by multiplying and dividing it. After 15 or so iterations it reaches such big/small numbers. Is there any way I can prevent such big numbers?

    This is the line that edits the values:

    output_arr = node * (input_arr[con1]/input_arr[con2])


    Is there a way of reducing the size? Maybe cap it at some value?

    Continue reading...

Compartilhe esta Página