Splunk Search

Typecast an integer to a float?

nick405060
Motivator
| makeresults | eval a=1024.0 | eval b=.15 | eval c=a*(1.0-b) | table a b c

gives

a   b   c
1024.0  0.15    870

There are no integers, only floats. So why does Splunk typecast "c" to an int? It's not like it typecasts 870.0 to an int (which would still be unacceptable) it does it to 870.4.

Also, how do I fix this so that 870.4 is displayed?

Tags (1)
0 Karma
1 Solution

back2root
Path Finder

Splunk by default rounds values within eval calculations to a precision that it thinks is appropriate. Thereby the resulting precision is limited by the precision of the least-precise operand.

To make it more precise let's focuse on the "(1.0-b)" part of your calculation:

Search:

| makeresults | eval b=.15 | eval c_part_optionA=(1.0-b) | eval c_part_optionB=(1.00-b) | eval c_part_optionC=exact((1.0-b)) | table b c_*

Results:

b c_part_optionA c_part_optionB c_part_optionC
0.15 0.9 0.85 0.85

So the solution to your problem is using the exact() function as dokumented in the SearchReference.

| makeresults | eval a=1024.0 | eval b=.15 | eval c=exact(a*(1.0-b)) | table a b c

This behaviour can be unexpected unexpected by an user. In my opinion the decision to do it that way is suboptimal and the behaviour is not documented verry well.

View solution in original post

HiroshiSatoh
Champion

It is rounded to an integer. If you want to control, use the following function.

ex.
| makeresults | eval a=1024.0 | eval b=.15 | eval c=round(a*(1.0-b),2) | table a b c

0 Karma

back2root
Path Finder

Splunk by default rounds values within eval calculations to a precision that it thinks is appropriate. Thereby the resulting precision is limited by the precision of the least-precise operand.

To make it more precise let's focuse on the "(1.0-b)" part of your calculation:

Search:

| makeresults | eval b=.15 | eval c_part_optionA=(1.0-b) | eval c_part_optionB=(1.00-b) | eval c_part_optionC=exact((1.0-b)) | table b c_*

Results:

b c_part_optionA c_part_optionB c_part_optionC
0.15 0.9 0.85 0.85

So the solution to your problem is using the exact() function as dokumented in the SearchReference.

| makeresults | eval a=1024.0 | eval b=.15 | eval c=exact(a*(1.0-b)) | table a b c

This behaviour can be unexpected unexpected by an user. In my opinion the decision to do it that way is suboptimal and the behaviour is not documented verry well.

nick405060
Motivator

Thanks. Definitely documented poorly, nothing in a Google search, and for something so basic too

0 Karma
Get Updates on the Splunk Community!

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...