0

I'm having trouble in a condition i want in a if statement. I want to check if a variable(float type), like 0.9, is multiple of 0.1.

I had this code: (t * dt) % 0.1 == 0

I've tried the modulo operator but gave me inaccurate results. For example 0.3 is not a multiple of 0.1. Can you help me?

5
  • See also: stackoverflow.com/questions/588004/… Commented Jan 29, 2024 at 13:08
  • @Guy that doesn't fully answer the question since it's a numpy array here (I doubt using Decimal would be a viable approach) Commented Jan 29, 2024 at 13:11
  • i understood the problem. can you recommend another way i can check if a variable is a multiple of 0.1? Commented Jan 29, 2024 at 13:11
  • @mariami you can use a = np.array([0.1, 0.2, 0.3]) ; n = 0.1 ; m = a%n ; out = np.isclose(m, 0) | np.isclose(m, n) Commented Jan 29, 2024 at 13:18
  • 1
    @mozway the OP tagged the question as numpy, but I don't see any other evidence this is really a numpy question. He/she even say variable(float type), like 0.9, is multiple of 0.1.. Commented Jan 29, 2024 at 13:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.