我实际上在" np.ceil"中遇到了一些问题。在python中。
<?php
namespace App\Providers;
use App\Post;
use App\Tag;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
protected $defer = true;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
View::composer('layouts.sidebar', function ($view) {
$view->with('archives', Post::archives());
$view->with('tags', Tag::has('posts')->pluck('name'));
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
我应该得到类似的东西:
import numpy as np
x_start = 0
y_start = 0
x_end = 2
y_end = 1
x_step = 0.4
y_step = 0.3
x_segment = int(np.ceil((x_end-x_start)/x_step))
y_segment = int(np.ceil((y_end-y_start)/y_step))
print "N. x: " + str(x_segment)
print "N. y: " + str(y_segment)
matrix = np.zeros((y_segment, x_segment), dtype=int)
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def frange(x, y, jump):
while x < y:
yield x
x += jump
for y in frange(y_start, y_end, y_step):
print "***"
for x in frange(x_start, x_end, x_step):
count = 0
print "(" + str(x) + "; " + str(y) + ") m[" + str(y_segment - int(np.ceil(y/y_step)) - 1) + "][" + str(int(np.ceil(x/x_step)))+"]" + " | x/x_step: " + str(x/x_step) + " | np.ceil(x/x_step): " + str(int(np.ceil(x/x_step)))
但数字&#34; 3&#34;被替换为&#34; 4&#34;。
...
***
(0; 0) m[3][0] | x/x_step: 0.0 | np.ceil(x/x_step): 0
(0.4; 0) m[3][1] | x/x_step: 1.0 | np.ceil(x/x_step): 1
(0.8; 0) m[3][2] | x/x_step: 2.0 | np.ceil(x/x_step): 2
(1.2; 0) m[3][3] | x/x_step: 3.0 | np.ceil(x/x_step): 3
(1.6; 0) m[3][4] | x/x_step: 4.0 | np.ceil(x/x_step): 4
***
...
你知道为什么吗?我该如何修复我的代码?谢谢!
答案 0 :(得分:0)
答案很长:
frange
似乎有一些浮点精度问题。看看当你完成frange
时会发生什么:
如果您只是打印原始花车:
>>> [x for x in frange(x_start, x_end, x_step)]
[0, 0.4, 0.8, 1.2000000000000002, 1.6]
出于某种原因,1.2
并不完全是1.2
。当你这样做时:
np.ceil(1.2000000000000002/x_step)
你得到4.0
(换句话说,np.ceil
可以正常工作)。
您想要的基本上是np.ceil(1.2/x_step)
,等于3.0
我建议您在应用np.round()
x
或类似内容来围绕np.ceil()
值