所以,我试图从浮点异常返回,但我的代码继续循环.我实际上可以退出该过程,但我想要做的是返回并重做导致浮点错误的计算.
发生FPE的原因是因为我有一个随机数生成器,它为多项式生成系数.使用一些LAPACK函数,我解决了根源并做了一些其他事情.在此数学密集型链中的某处,会发生浮点异常.当发生这种情况时,我想要做的是递增随机数生成器状态,并再次尝试直到系数使得错误没有实现,因为它通常不会,但很少会导致灾难性的结果.
所以我写了一个简单的测试程序来学习如何处理信号.它在下面:
在exceptions.h中
#ifndef EXCEPTIONS_H
#define EXCEPTIONS_H
#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <math.h>
#include <errno.h>
#include <float.h>
#include <fenv.h>
void overflow_handler(int);
#endif // EXCEPTIONS_H //
Run Code Online (Sandbox Code Playgroud)
在例外情况下
#include "exceptions.h"
void overflow_handler(int signal_number)
{
if (feclearexcept(FE_OVERFLOW | FE_UNDERFLOW | FE_DIVBYZERO | FE_INVALID)){
fprintf(stdout, "Nothing Cleared!\n");
}
else{
fprintf(stdout, "All Cleared!\n");
}
return;
}
Run Code Online (Sandbox Code Playgroud)
在main.c
#include "exceptions.h"
int main(void)
{
int failure;
float oops;
//===Enable Exceptions===//
failure = 1;
failure = feenableexcept(FE_OVERFLOW | FE_UNDERFLOW | FE_DIVBYZERO …Run Code Online (Sandbox Code Playgroud) 所以我需要计算N个变量的联合概率分布.我有两个变量的代码,但我很难将它推广到更高的维度.我想有一些pythonic矢量化可能会有所帮助,但是,现在我的代码非常像C(是的,我知道这不是编写Python的正确方法).我的2D代码如下:
import numpy
import math
feature1 = numpy.array([1.1,2.2,3.0,1.2,5.4,3.4,2.2,6.8,4.5,5.6,1.9,2.8,3.7,4.4,7.3,8.3,8.1,7.0,8.0,6.8,6.2,4.9,5.7,6.3,3.7,2.4,4.5,8.5,9.5,9.9]);
feature2 = numpy.array([11.1,12.8,13.0,11.6,15.2,13.8,11.1,17.8,12.5,15.2,11.6,20.8,14.7,14.4,15.3,18.3,11.4,17.0,16.0,16.8,12.2,14.9,15.7,16.3,13.7,12.4,14.2,18.5,19.8,19.0]);
#===Concatenate All Features===#
numFrames = len(feature1);
allFeatures = numpy.zeros((2,numFrames));
allFeatures[0,:] = feature1;
allFeatures[1,:] = feature2;
#===Create the Array to hold all the Bins===#
numBins = int(0.25*numFrames);
allBins = numpy.zeros((allFeatures.shape[0],numBins+1));
#===Find the maximum and minimum of each feature===#
allRanges = numpy.zeros((allFeatures.shape[0],2));
for f in range(allFeatures.shape[0]):
allRanges[f,0] = numpy.amin(allFeatures[f,:]);
allRanges[f,1] = numpy.amax(allFeatures[f,:]);
#===Create the Array to hold all the individual feature probabilities===#
allIndividualProbs = numpy.zeros((allFeatures.shape[0],numBins));
#===Grab all the Individual Probs and …Run Code Online (Sandbox Code Playgroud) 我在使用CBLAS执行外部产品时遇到问题.我的代码如下:
//===SET UP===//
double x1[] = {1,2,3,4};
double x2[] = {1,2,3};
int dx1 = 4;
int dx2 = 3;
double X[dx1 * dx2];
for (int i = 0; i < (dx1*dx2); i++) {X[i] = 0.0;}
//===DO THE OUTER PRODUCT===//
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasTrans, dx1, dx2, 1, 1.0, x1, dx1, x2, 1, 0.0, X, dx1);
//===PRINT THE RESULTS===//
printf("\nMatrix X (%d x %d) = x1 (*) x2 is:\n", dx1, dx2);
for (i=0; i<4; i++) {
for (j=0; j<3; j++) {
printf ("%lf …Run Code Online (Sandbox Code Playgroud) 因此,我试图使用C在GTK3 +中创建一个简单的菜单。我试图重新创建位于以下位置的菜单:
https://developer.gnome.org/gtk-tutorial/stable/x743.html
因此,我复制了代码,并一直试图隔离我需要的部分,并将这些部分隐蔽到3+。我对这个代码段不太满意。首先,不建议使用gtk_option_menu,并用gtk_combo_box代替。我试图找到与此相关的教程,但没有任何结果。
老实说,我什至不知道如何用GTK创建菜单的背后原理,所以对我来说转换非常困难。我在这里几乎失明了。
我的代码如下:
#include "/usr/include/gtk-3.0/gtk/gtk.h"
static void print_stuff( GtkWidget *item, gpointer data) {
printf("Hello World!\n");
return;
}
static GtkWidget *make_menu_item(gchar *name, GCallback callback, gpointer data ){
GtkWidget *item;
item = gtk_menu_item_new_with_label (name);
g_signal_connect (item, "activate", callback, (gpointer) data);
gtk_widget_show (item);
return item;
}
static void create_range_controls( void ){
GtkWidget *window;
GtkWidget *box1, *box2, *box3;
GtkWidget *button;
GtkWidget *scrollbar;
GtkWidget *separator;
GtkWidget *opt, *menu, *item;
GtkWidget *label;
GtkWidget *scale;
GObject *adj1, *adj2;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), …Run Code Online (Sandbox Code Playgroud)