首页 > 编程语言 >Scala中实现和Python一致的整数除法和整数求余

Scala中实现和Python一致的整数除法和整数求余

时间:2023-05-17 14:02:50浏览次数:35  
标签:Scala Python same sign Int 整数

\[\color{black}{\text{In scala, it's weird to mimic `%` `//` of python}} \]

/*
Python's % operator returns a result with the same sign as the divisor, and // rounds towards negative infinity.

In Scala, % and / don't behave the same way. The % operator returns a result with the same sign as the dividend, and / performs truncating division, rounding towards zero.
*/


def pythonMod(a: Int, b: Int): Int = ((a % b) + b) % b

def pythonDiv(a: Int, b: Int): Int = {
  if ((a > 0 && b > 0) || (a < 0 && b < 0)) a / b
  else if (a % b == 0) a / b
  else a / b - 1
}

标签:Scala,Python,same,sign,Int,整数
From: https://www.cnblogs.com/yhm138/p/17408540.html

相关文章

  • python:ERROR: Could not build wheels for wordcloud, which is required to install
    pycharm里无法下载,在下面下载出现问题 需要下载error里的文件https://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud这个网站找。输入Python,看自己电脑是怎样的 下载文件后,放到对应位置,下载成功 ......
  • Python枚举类型enum
    为什么需要枚举枚举(Enum)是一种数据类型,也是一种特别的类,是绑定到唯一值的符号表示,可以使用它来创建用于变量和属性的常量集枚举类可以看成是一个下拉菜单,给出特定的选项且这些选项不可修改,更贴近自然语言的方式表达数据,可以让代码更容易阅读、维护,减少转换或者错误值引......
  • 48024 python 代码分析
    ComputerScience220SC(2023)Assignment3(Basicgraphalgorithmsandtraversals)SeeCanvasforduedateThisassignmentrequiresyoutosubmitprogramsinPythonthatyouhavewrittenyourselftotheautomarker,https://www.automarker.cs.auckland.ac.nz.Yo......
  • 【Python】Centos7安装dirsearch
    一、升级Openssl1.1.1 1、官网下载源码:https://www.openssl.org/2、安装#./config--prefix=/datas/soft/openssl-1.1.1no-zlib#make#makeinstall3、新版配置#ln-s/datas/soft/openssl-1.1.1/include/openssl/usr/include/openssl#ln-s/datas/soft/openss......
  • < Python全景系列-3 > Python控制流程盘点及高级用法、神秘技巧大揭秘!
    欢迎来到我们的系列博客《Python全景系列》!在这个系列中,我们将带领你从Python的基础知识开始,一步步深入到高级话题,帮助你掌握这门强大而灵活的编程语法。无论你是编程新手,还是有一定基础的开发者,这个系列都将提供你需要的知识和技能。 这是系列第三篇,在这篇文章中我们将全面深......
  • python 使用企业微信发邮件加加图形化界面
    importsysfromPyQt5.QtWidgetsimportQApplication,QWidget,QLabel,QLineEdit,QTextEdit,QPushButtonimportrequests,base64,hashlibimportsmtplibimportosimportglobfromemail.mime.textimportMIMETextfromemail.mime.multipartimportMIMEMultipa......
  • generate a 3D chart in Python using the CSV data
    Here's an example of how you could use Matplotlib to create a 3D scatter plot from your CSV data:   importpandasaspdimportmatplotlib.pyplotaspltfrommpl_toolkits.mplot3dimportAxes3D#LoadtheCSVdataintoaPand......
  • 用Python开发输入法后台
    首先,安装PIME,github地址:https://github.com/EasyIME/PIME下载安装PIME-1.3.0-stable-setup.exe,就可以得到一个输入法,后端是python,在安装目录下‘C:\ProgramFiles(x86)\PIME\’,python目录就是运行脚本。入口点是'server.py',其中安装目录下的‘PIMELauncher.exe’是负责前后台......
  • python 项目报错 Fatal Python error: _enter_buffered_busy: could not acquire lock
    FatalPythonerror:_enter_buffered_busy:couldnotacquirelockfor<_io.BufferedWritername=''>atinterpretershutdown,possiblyduetodaemonthreadsPythonruntimestate:finalizing(tstate=0x00005654c4008a40)Currentthread0x00007fc......
  • Python学习
    3-13字符串类型字符串类型:str   1.定义格式:       变量='内容'           打印一行       变量="内容"           打印一行       变量='''内容'''或者三引号           可以通过回车的方式换行,且打印出......