Community discussions

 
changeip
Forum Guru
Forum Guru
Topic Author
Posts: 3803
Joined: Fri May 28, 2004 5:22 pm

Decimals ?

Wed Jun 11, 2008 9:21 pm

Doesn't MT support decimal places ??

[admin@MikroTik] interface> :put (5 / 2)
2

Since when did 5 / 2 = 2 ?

I'm trying to parse the GPS coordinates and convert them back to decimal. Here is a sample script but I cannot get any further since the math is broken.
:global gps [/system gps monitor as-value]

:put ($gps -> "date-and-time")
:put ($gps -> "longitude")

# Parse Longitude
:global long1 [:find ($gps -> "longitude") " " 0]
:global long2 [:find ($gps -> "longitude") " " $long1]
:global long3 [:find ($gps -> "longitude") "'" $long2]
:global long4 [:find ($gps -> "longitude") "''" $long3]

:global degrees [:tonum [:pick ($gps -> "longitude") $long1 $long2 ] ]
:global minutes [:tonum [:pick ($gps -> "longitude") $long2 $long3 ] ]
:global seconds [:tonum [:pick ($gps -> "longitude") ($long3 + 2) $long4 ] ]

:env print

# Should give decimal longitude.
:put ($degrees + ($minutes/60) + ($seconds/3600))

I end up with this:

"gps"={"date-and-time"="jun/11/2008 18:19:25"; "longitude"="W 117 12' 55''"; "latitude"="N 33 13' 49''"; "altitude"="191.399994m"; "speed"="0.111120 km/h"; "valid"=true}
"long1"=1
"long2"=5
"long3"=8
"long4"=12
"degrees"=117
"minutes"=12
"seconds"=55

[master@cip-home] /system gps> :put ($degrees + ($minutes/60) + ($seconds/3600))
117


What happened to my decimals!

If I can get this working I have a script that will upload your routers GPS location to GpsGate.com servers and so you can keep track of your whereabouts on all gps-enabled routers.

Sam
Colo and Wholesale Bandwidth Available! Sales at SanDiegoBroadband dot com
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5921
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Decimals ?

Wed Jun 11, 2008 11:07 pm

Math is not broken. Router os support only integers.
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: Decimals ?

Wed Jul 22, 2009 1:06 am

This will calculate exact decimal values to any decimal places you want. Just plug in dividend, divisor, and decimal places and $result variable will be set to the answer.
# Preforms calculation with decimal points
# dividend / divisor = quotient.decimal = result

:local dividend 3
:local divisor 11
:local decimalplaces 3


# Math Calculation here
:local quotient 0
:local remainder 0
:local result 0
:local decimal 0

:set quotient ($dividend / $divisor)

:if ($quotient = 0) do={
  :set dividend [:tonum ($dividend . "0")]
}

:set remainder ($dividend - ($divisor * $quotient))

:if ($remainder > 0) do={
  :local tmpremainder [:tonum ($remainder . "0")]
  :for x from=1 to=$decimalplaces do={
    :local tmpdecimal [:tonum ($tmpremainder / $divisor)]
    :set decimal [:tonum ($decimal . $tmpdecimal)]
    :set tmpremainder [:tonum (($tmpremainder - ($tmpdecimal * $divisor)) . "0")]
  }
  :set result ($quotient . "." . $decimal)
} else={
  :set result $quotient
}
# END Math Calculation here

:put ($dividend . " / " . $divisor . " = " . $result)

Hope this helps.
Doug
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1296
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: Decimals ?

Fri Aug 30, 2019 6:17 pm

Math is not broken. Router os support only integers.
2019 Is this still true?
{
local speed 10
:put $speed
:local speedknots ($speed * 5)
:put $speedknots
}
10
50
{
local speed 10
:put $speed
:local speedknots ($speed * 1.5)
:put $speedknots
}
10
Script Error: cannot multiply time interval by ip prefix
{
local speed 10
:put $speed
:local speedknots ($speed * 1,5)
:put $speedknots
}
10
10;5
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5921
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Decimals ?

Mon Sep 02, 2019 10:55 am

Yes only integers. Anywhere where you see decimal representation is actually a string.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1296
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: Decimals ?

Mon Sep 02, 2019 7:43 pm

I know, but MT could add BC or other Linux tool to the script to handle decimal.

Lokking forward to v 7.0 :)
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 

Who is online

Users browsing this forum: No registered users and 4 guests