N
N
nurzhannogerbek2019-07-17 09:00:30
go
nurzhannogerbek, 2019-07-17 09:00:30

How to compare two *float64 values ​​in GO?

Hello comrades! Please help me figure it out.
There are two variables. Let's call them GLOBAL_PERCENT and LOCAL_PERCENT . The value in them is dynamic. Both have data type *float64 .
Tasks :
1) If the value in the "LOCAL_PERCENT" variable is less than 5 compared to the value in the GLOBAL_PERCENT variable, then print the word RED to the console.
2) If the value in the LOCAL_PERCENT variable is 1-5 less than the value in the GLOBAL_PERCENT variable, then output the word YELLOW to the console.
3) If the value in the LOCAL_PERCENT variable is greater than 5 compared to the value in the GLOBAL_PERCENT variable, then print the word GREEN to the console.
An attempt to use the code below resulted in an error. Error: operator < not defined on *float64. The question arises how to correctly compare two *float64 values ​​with each other in the Golang programming language?

if LOCAL_PERCENT < GLOBAL_PERCENT * 0.95 {
  println("RED")
} else if LOCAL_PERCENT > GLOBAL_PERCENT * 0.95 {
  println("GREEN")
} else {
  println("YELLOW")
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Shitskov, 2019-07-17
@nurzhannogerbek

*float64 is a pointer to the float64 type
. This is done using the * operator.
if *LOCAL_PERCENT < *GLOBAL_PERCENT

G
GromderCom, 2020-02-21
@GromderCom

There are several detailed articles on working with numbers in Golang: Float , Integer and large numbers . The first article has a detailed description of all the required points.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question