GO-SDK标准库2:math

本文介绍使用Go语言中的math/big包进行大整数运算的方法,包括加减乘除等基本操作。通过具体实例展示了如何创建大整数并执行各种算术运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package main

import (
    "math/big"
    "fmt"
)

func main() {

    //创建大数(值可以突破int64)
    bigInt1 := big.NewInt(123)
    bigInt2 := new(big.Int)
    bigInt2.SetString("314159265358979323846264338327950288419716939937510582097494459", 10)
    fmt.Printf("type=%T,value=%v\n",bigInt1,bigInt1)
    fmt.Printf("type=%T,value=%v\n",bigInt2,bigInt2)

    //大数的计算
    bigx := big.NewInt(1)
    big1 := big.NewInt(11)
    big2 := big.NewInt(3)
    fmt.Println(bigx.Add(big1, big2),bigx)//14
    fmt.Println(bigx.Sub(big1, big2),bigx)//8
    fmt.Println(bigx.Mul(big1, big2),bigx)//33
    fmt.Println(bigx.Div(big1, big2),bigx)//3
    fmt.Println(bigx.Mod(big1, big2),bigx)//2
    fmt.Println(bigx.And(big1, big2),bigx)//3
    fmt.Println(bigx.Or(big1, big2),bigx)//11
    fmt.Println(bigx.Xor(big1, big2),bigx)//8

    //每一步的结果都重新给bigx赋值,所以事实上得到的是最后一步的结果
    fmt.Println(bigx.Add(big1,big2).Sub(big1,big2).Mul(big1,big2).Div(big1,big2))

    //(11+3)*2%3=1
    fmt.Println(bigx.Mod((bigx.Mul((bigx.Add(big1, big2)),big.NewInt(2))),big.NewInt(3)))
}

 

#!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys import math import rospy from std_srvs.srv import Trigger from jetauto_sdk.mecanum import MecanumChassis from geometry_msgs.msg import Twist, TransformStamped class JetAutoController: def __init__(self): rospy.init_node('jetauto_controller', anonymous=False) self.last_linear_x = 0 self.last_linear_y = 0 self.last_angular_z = 0 self.mecanum = MecanumChassis(a =103, b=97, wheel_diameter=96.5, pulse_per_cycle=4320)#44.0 * 178.0 self.machine_type = os.environ.get('MACHINE_TYPE') self.go_factor = rospy.get_param('~go_factor', 1.00) self.turn_factor = rospy.get_param('~turn_factor', 1.00) rospy.Subscriber('jetauto_controller/cmd_vel', Twist, self.cmd_vel_callback) rospy.Subscriber('cmd_vel', Twist, self.app_cmd_vel_callback) rospy.Service('jetauto_controller/load_calibrate_param', Trigger, self.load_calibrate_param) def load_calibrate_param(self, msg): self.go_factor = rospy.get_param('~go_factor', 1.00) self.turn_factor = rospy.get_param('~turn_factor', 1.00) rospy.loginfo('load_calibrate_param') return [True, 'load_calibrate_param'] def app_cmd_vel_callback(self, msg): if msg.linear.x > 0.2: msg.linear.x = 0.2 if msg.linear.x < -0.2: msg.linear.x = -0.2 if msg.angular.z > 0.5: msg.angular.z = 0.5 if msg.angular.z < -0.5: msg.angular.z = -0.5 self.cmd_vel_callback(msg) def cmd_vel_callback(self, msg): linear_x = self.go_factor*msg.linear.x linear_y = self.go_factor*msg.linear.y angular_z = self.turn_factor*msg.angular.z speed_up = False if abs(self.last_linear_x - linear_x) > 0.2 or abs(self.last_linear_y - linear_y) > 0.2 or abs(self.last_angular_z - angular_z) > 1: speed_up = True self.last_linear_x = linear_x self.last_linear_y = linear_y self.last_angular_z = angular_z linear_x_, linear_y_ = linear_x * 1000.0, linear_y * 1000.0#mm to m speed = math.sqrt(linear_x_ ** 2 + linear_y_ ** 2) direction = math.atan2(linear_y_, linear_x_) direction = math.pi * 2 + direction if direction < 0 else direction self.mecanum.set_velocity(speed, direction, angular_z, speed_up=speed_up) if __name__ == "__main__": try: node = JetAutoController() rospy.spin() except Exception as e: rospy.logerr(str(e)) finally: node.mecanum.reset_motors() sys.exit()
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值