小编Mil*_*avi的帖子

Python中的浮点精度

我在python中编写了这个函数:

import math

def radical(a, b, k): 
    return (1-((a**2-b**2)/a**2)*(math.sin(math.pi*(2*k-1)/180))**2)**.5

def f(a, b): 
 sigma = 0 
 for k in range(1,180/4):
    sigma = sigma + radical(a, b, k)
 return 8*a*math.sin(math.pi/180)*sigma

print f(25.,35.)
Run Code Online (Sandbox Code Playgroud)

当我在Wolphramapha和Maple中计算这个函数时,我会得到189.797但是使用python 我会得到184.91089913 我的程序有什么问题?

python math floating-point precision floating-point-precision

2
推荐指数
1
解决办法
1342
查看次数

如何创建无限递归会抛出堆栈溢出异常?

我正在尝试使用以下程序创建堆栈溢出运行时异常:

void f(int a) {
  cout << a << ", ";
  f(++a);
}

int main () {
  f(0);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行此程序时,我的计算机运行大约261824 call stack然后command terminated发生运行时错误.现在我想知道:

  1. 这是堆栈溢出的一个很好的例子吗?如果是,为什么command terminated会出现错误?
  2. 我怎么能try,catch堆栈溢出异常?
  3. 我有很多自由记忆; 为什么我的堆栈不会占用我所有的记忆?
  4. 如何确定堆栈的大小与我的对应call stack

c++ stack-overflow stack callstack exception

2
推荐指数
1
解决办法
535
查看次数

为什么当我使用copy_from_user时,一些模糊的字符添加到原始缓冲区?

我在内核模块中创建了WRITE_IOCTL,并在用户模式下调用它:

ioctl(fd, WRITE_IOCTL, "Hello, Kernel!");
Run Code Online (Sandbox Code Playgroud)

在内核模式中,我有:

static int device_ioctl(struct file *filp,
    unsigned int cmd, unsigned long args) {
  char buff[14];

  switch (cmd) {
  case WRITE_IOCTL:
    copy_from_user( buff,(char *)args, 14);
    printk("This message received from User Space: %s\n", buff);
    break;
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行这个ioctl时,我在/var/log/kern.log中有类似的东西:

This message received from User Space: Hello, Kernel!vE?
This message received from User Space: Hello, Kernel!M?
This message received from User Space: Hello, Kernel!M?
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题??

c kernel kernel-module linux-device-driver linux-kernel

2
推荐指数
1
解决办法
1340
查看次数

如何使WebView高度尺寸与屏幕高度相符?

如果我的WebView太大,我没有问题,即使内容如此之大,WebView高度也适合屏幕高度我可以滚动WebView的内容.但是如果WebView的内容很少,则WebView高度不适合屏幕.

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<ScrollView android:layout_width="fill_parent"
    android:layout_height="fill_parent"
        android:fitsSystemWindows="true">

    <WebView android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fitsSystemWindows="true" />

</ScrollView>

<LinearLayout android:id="@+id/media_player"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="visible">

    <Button android:textStyle="bold"
        android:id="@+id/ButtonPlayStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/ic_media_play" />

    <SeekBar android:id="@+id/SeekBar"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@id/ButtonPlayStop" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是截图:

在此输入图像描述

任何人都可以帮我解决这个问题?

android webview android-layout android-gui

1
推荐指数
1
解决办法
1万
查看次数

在Prolog中,Negation-as-Failure如何运作?

我想知道Prolog如何解决这个程序:

test(X, Y).
test(X, X):-!, fail.
Run Code Online (Sandbox Code Playgroud)

我用谷歌搜索"否定为失败",但我很困惑!

prolog

1
推荐指数
1
解决办法
4060
查看次数

为什么在c ++中使用内联函数不会增加二进制大小?

我写了这段代码:

inline int a_plus_b_power2(int a, int b) {
  return (a + b) * (a + b); 
}

int main() {
  for(int a = 0; a < 9999999999999; ++a)
    for(int b = 0; b < 999999999999; ++b)
      a_plus_b_power2(a, b);  

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是为什么这个程序的二进制文件与这个程序没有区别:

inline int a_plus_b_power2(int a, int b) {
  return (a + b) * (a + b); 
}

int main() {
  for(int a = 0; a < 9; ++a)
    for(int b = 0; b < 9; ++b)
      a_plus_b_power2(a, b);  

  return …
Run Code Online (Sandbox Code Playgroud)

c++ inline-functions

0
推荐指数
1
解决办法
200
查看次数

为什么我的makefile中的自动变量找不到任何目标?

我写了这个问候世界hello.c:

#include <stdio.h>

int main() {
  printf("Hello, World!\n");
  exit( 0 );
}
Run Code Online (Sandbox Code Playgroud)

Makefile是:

%: %.c
Run Code Online (Sandbox Code Playgroud)

当我运行时,make我会收到此错误:make: *** No targets. Stop.

makefile gnu-make

0
推荐指数
1
解决办法
198
查看次数

C++链接器错误Singleton类中的未定义引用

我正在尝试将此java单例类移植到c ++:

public class Singleton {
private static Singleton uniqueInstance;

private Singleton() {} 
public static Singleton getInstance() {
    if (uniqueInstance == null) {
        uniqueInstance = new Singleton();
    }
        return uniqueInstance;
    }
}
Run Code Online (Sandbox Code Playgroud)

我移植到这个C++代码:

class Singleton {
private:
  Singleton() {
    cout << "new Singleton is called" << endl;
  }
  static Singleton* uniqueInstance;
public:
   static Singleton* getInstance() {
    if (!uniqueInstance) {
      uniqueInstance = new Singleton();
    }
    return uniqueInstance;
  }
};
Run Code Online (Sandbox Code Playgroud)

但是我无法编译这个!和gcc链接器发生错误.

c++ singleton design-patterns linker-errors

-1
推荐指数
1
解决办法
3130
查看次数

How to write Hello World in pure c (Without using any c library)?

I know that linux kernel source is in pure c. So I want to know how can I write simple Hello, World program in pure C without using printf api?

c

-2
推荐指数
1
解决办法
689
查看次数