首页 \ 问答 \ 不确定for循环迭代?(Unsure about for-loop iteration?)

不确定for循环迭代?(Unsure about for-loop iteration?)

所以我刚开始用Java编程,我只是很难理解为什么会这样

for (int i = 0, j=0; i <10; i++) {
    System.out.println(j += j++);
}

打印出10次?


So I'm new to programming in Java and I'm just having a hard time understanding why this

for (int i = 0, j=0; i <10; i++) {
    System.out.println(j += j++);
}

prints out 0 ten times?


原文:
更新时间:2022-04-27 10:04

最满意答案

通过使用产品与总和触发器。 身份 ,你可以减少三角形的数量。 函数调用。 在下面, func1func2计算相同的值,但func2减少了触发的调用。 功能。

import numpy as np

def func1(a, b, c, d):
    A = np.cos(a) * np.cos(b) - np.sin(a) * np.sin(b) * np.sin(c - d)
    return A

def func2(a, b, c, d):
    s = np.sin(c - d)
    A = 0.5*((1 - s)*np.cos(a - b) + (1 + s)*np.cos(a + b))
    return A

这是N = 5000的时序比较:

In [48]: %timeit func1(a, b, c, d)
1000 loops, best of 3: 374 µs per loop

In [49]: %timeit func2(a, b, c, d)
1000 loops, best of 3: 241 µs per loop

By using the product-to-sum trig. identities, you can reduce the number of trig. function calls. In the following, func1 and func2 compute the same value, but func2 makes fewer calls to trig. functions.

import numpy as np

def func1(a, b, c, d):
    A = np.cos(a) * np.cos(b) - np.sin(a) * np.sin(b) * np.sin(c - d)
    return A

def func2(a, b, c, d):
    s = np.sin(c - d)
    A = 0.5*((1 - s)*np.cos(a - b) + (1 + s)*np.cos(a + b))
    return A

Here's a timing comparison with N = 5000:

In [48]: %timeit func1(a, b, c, d)
1000 loops, best of 3: 374 µs per loop

In [49]: %timeit func2(a, b, c, d)
1000 loops, best of 3: 241 µs per loop

相关问答

更多
  • 下面是关于如何做三角函数的幂级数近似(不是泰勒级数)的一些很好的幻灯片: http : //www.research.scea.com/gdc2003/fast-math-functions.html 它面向游戏程序员,这意味着准确性会牺牲性能,但是您应该能够在近似值中添加另外一个或两个术语,以获得一些准确度。 关于这一点的好处是,您还应该能够轻松地将其扩展到SIMD,以便您可以在一个(如果使用双精度时为2)计算4个值的正弦或余弦。 希望这有助于... Here are some good slides o ...
  • 这是一个数学问题,而不是Swift问题: let sinus = sin(90.0 * M_PI / 180) print("Sinus \(sinus)") let cosinus = cos(90 * M_PI / 180) print("Cosinus \(cosinus)") let tangent = tan(90 * M_PI / 180) print("Tangent \(tangent)") 版画 Sinus 1.0 Cosinus 6.12323399573677e-17 Tange ...
  • 首先,你必须做一些减小范围。 Trig函数是周期性的,因此您需要将参数减少到标准间隔。 对于初学者,您可以将角度减小到0到360度之间。 但是通过使用几个身份,你意识到你可以得到更少的。 如果您计算0和45度之间角度的正弦和余弦,则可以引导您计算所有角度的所有触发功能。 一旦你减少了争论,大多数芯片都使用CORDIC算法来计算正弦和余弦。 你可能会听到有人说电脑使用泰勒系列。 这听起来很合理,但不是这样。 CORDIC算法更适合高效的硬件实现。 ( 软件库可能使用泰勒系列,在不支持触发功能的硬件上说)。可能 ...
  • 当使用浮点数组时,可以使用np.einsum - np.sqrt(np.einsum('ij,ij->i',a,a)) 运行时测试 - In [34]: a = np.random.rand(1000,1000) In [35]: np.allclose(np.sum(a**2, axis=1)**.5,np.sqrt(np.einsum('ij,ij->i',a,a))) Out[35]: True In [36]: %timeit np.sum(a**2, axis=1)**.5 100 loop ...
  • 不到Radians <==> toDegrees()工作吗? 您可以使用这些方法将度数转换为弧度,反之亦然 Doesn't toRadians <==> toDegrees() work ? You can use these methods to convert from degrees to radians and the other way around
  • 1.2246467991473532e-16接近于0 - 小数点和第一个有效数字之间有16个零 - 很像3.1415926535897931 ( math.pi的值)接近于pi。 答案是正确的小数点后16位! 所以如果你想让sin(pi)等于0,只需将其舍入到合理的小数位数。 15对我来说看起来不错,对于任何应用程序都应该足够多: print round(math.sin(math.pi), 15) 1.2246467991473532e-16 is close to 0 -- there are 16 ...
  • 根据所需的速度和精度要求,您可以通过编写程序来创建查找表,从而创建简单查找表所需的功能。 或者使用CORDIC 。 Depending on the needed requirements for speed and precision, maybe you could create the functions needed by a simple lookup table, by writing a program to create the lookup table. Or use CORDIC.
  • 问题的范围很广,但这里有一些简单的想法(和代码!)可以作为计算arctan的起点。 首先,好老泰勒系列。 为简单起见,我们使用固定数量的术语; 实际上,您可能希望根据x的大小来决定动态使用的术语数,或者引入某种收敛准则。 使用固定数量的术语,我们可以使用类似于Horner方案的东西进行有效评估。 def arctan_taylor(x, terms=9): """ Compute arctan for small x via Taylor polynomials. Uses a f ...
  • 由于用于计算三角函数的大多数(on-die)算法使用CORDIC的一些变体,我敢打赌,无论如何,这些值在trig函数调用的入口点处被限制在[0,Pi / 2)之内。 这就是说,如果你有一种方法可以在整个算法中保持角度接近于零,那么这样做可能是明智的。 实际上,sin(10 ^ 42)的值几乎是不确定的,因为10 ^ 42范围内的粒度大约为10 ^ 25。 这意味着例如如果你要增加角度,并且如果这样做,它们可以变大,那么你应该考虑定期夹紧它们。 但在三角函数调用之前将它们夹紧是不必要的。 Since most ...
  • 通过使用产品与总和触发器。 身份 ,你可以减少三角形的数量。 函数调用。 在下面, func1和func2计算相同的值,但func2减少了触发的调用。 功能。 import numpy as np def func1(a, b, c, d): A = np.cos(a) * np.cos(b) - np.sin(a) * np.sin(b) * np.sin(c - d) return A def func2(a, b, c, d): s = np.sin(c - d) ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)