我收到的最近的家庭作业要求我们采取表达式,这些表达式可能会在计算机中执行时造成精度损失,并对其进行更改以避免这种损失.
不幸的是,这样做的方向尚未明确.通过观察正在执行的各种示例,我知道有一些方法可以做到这一点:使用泰勒级数,如果涉及平方根则使用共轭,或者在减去两个分数时找到共同的分母.
但是,我很难注意到何时会发生精度损失.到目前为止,我唯一确定的是,当你减去两个接近相同的数字时,由于高阶数字很重要,你会失去精确度,而你会因为四舍五入而失去精确数字.
我的问题是我应该寻找的其他常见情况,以及接近它们的"好"方法是什么?
例如,这是一个问题:
f(x) = tan(x) ? sin(x) when x ~ 0
Run Code Online (Sandbox Code Playgroud)
在这三种选择中评估这一点的最佳和最差算法是什么:
(a) (1/ cos(x) ? 1) sin(x),
(b) (x^3)/2
(c) tan(x)*(sin(x)^2)/(cos(x) + 1).
Run Code Online (Sandbox Code Playgroud)
据我所知,当x接近于零时,tan(x)和sin(x)几乎相同.我不明白为解决这个问题,这些算法中的任何一个或更好的方式或原因.
MSDN声明该方法返回
如果方法成功排队,则为true;否则为false.如果工作项未排队,则抛出NotSupportedException.
为了测试目的,如何让方法返回false?或者它只是一个"次优"的类设计?
我有一个.NET应用程序,在批量导入中处理大约300,000条记录,每条记录需要几秒钟,所以我想并行化这个.在下面的代码中,ProcessWithAnsycDelegates()和之间有什么区别ProcessWithThreadPool()?
public class ResultNotification
{ public EventHandler event Success;
public EventHandler event Fail;
internal void Notify(bool sucess) {if (success) Success(); else Fail();}
}
public static class Processor
{ public ResultNotification ProcessWithAnsycDelegates(Record record)
{ var r = new ResultNotification();
Func<Record,bool> processRecord=new RecordProcessor().ProcessRecord;
processRecord.BeginInvoke
( record
,ar => result.Notify(processRecord.EndInvoke(ar))
,null);
return r;
}
public ResultNotification ProcessWithThreadPool(Record r)
{ var r = new ResultNotification();
var rp = new RecordProcessor();
ThreadPool.QueueWorkUserItem(_=>result.Notify(rp.ProcessRecord(r)));
return r;
}
}
Run Code Online (Sandbox Code Playgroud) 为了学习和演示,我需要一个宏来打印其参数并对其进行评估.我怀疑这是一个非常常见的情况,甚至可能是常见问题,但我找不到实际的参考.
我目前的代码是:
#define PRINT(expr) (fprintf(stdout, "%s -> %d\n", __STRING(expr), (expr)))
Run Code Online (Sandbox Code Playgroud)
然后:
PRINT(x & 0x01);
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我不确定__STRING宏的合法状态,特别是因为它在private __命名空间中.
所以,我的问题:
我想在datetime值上添加秒(00:00:02)或分钟(00:00:20)(可能存储字符串类型)但是如何?例子:
13:30+02:02:02= 15:32:02 ,
13:30+00:00:01= 13:30:01 ,
13:30+00:01:00=13:31:00 or 13:30 (not important)
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗?我需要你的酷算法:)再次感谢...
如何构建一个面积有效的电路,使用4输入LUT(查找表)计算15位输入中的设置位数.输出显然是4位(计数0-15).有人声称可以使用9个LUT.
我的Django应用程序中有一个ModelForm,它使用forms.ModelMultipleChoiceField,它在窗体上显示为forms.CheckboxSelectMultiple小部件.此ModelForm用于选择/取消选择多对多关系的值.这是问题所在:当您取消选中所有复选框并保存表单时,它不会保存.如果取消选中除1之外的所有值,它会保存正确.
这里有关于模型形式和多对多关系的技巧吗?我遇到了一个bug吗?我是Django的新手.提前致谢.
自定义字段:
class NetworkMessageChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
return obj.display_message
Run Code Online (Sandbox Code Playgroud)
型号表格:
class MessageTemplateForm(forms.ModelForm):
network_messages = NetworkMessageChoiceField(queryset=NetworkMessageTemplate.objects,
widget=forms.CheckboxSelectMultiple())
class Meta:
model = UserProfile
fields = ('network_messages',)
Run Code Online (Sandbox Code Playgroud)
查看保存表单:
def save_message_templates(request, extra_context=dict()):
try:
profile_obj = request.user.get_profile()
except ObjectDoesNotExist:
profile_obj = UserProfile(user=request.user)
if request.method == 'POST':
form = MessageTemplateForm(request.POST, instance=profile_obj)
if form.is_valid():
form.save()
return redirect('/')
return index(request, message_template_form=form)
Run Code Online (Sandbox Code Playgroud)
编辑:
我的表单字段缺少必需= False.
class MessageTemplateForm(forms.ModelForm):
network_messages = NetworkMessageChoiceField(queryset=NetworkMessageTemplate.objects,
widget=forms.CheckboxSelectMultiple(),
required=False)
class Meta:
model = UserProfile
fields = ('network_messages',)
Run Code Online (Sandbox Code Playgroud) 我正在实施UIButton,我的UIControlEventTouchUpInside活动并没有开火,即使UIControlEventTouchDown开火了.
UIButton *btnClose = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *filePathImage = @"img.png";
NSString *filePathImageTap = @"img-tap.png";
UIImage *buttonImage = [UIImage imageWithContentsOfFile:filePathImage];
UIImage *buttonImageTap = [UIImage imageWithContentsOfFile:filePathImageTap];
[btnClose setImage:buttonImage forState:UIControlStateNormal];
[btnClose setImage:buttonImageTap forState:UIControlStateSelected];
[btnClose setImage:buttonImageTap forState:UIControlStateHighlighted];
[btnClose addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnClose];
Run Code Online (Sandbox Code Playgroud) 我们正在为学校开展一个项目,我们有2个PIR运动传感器在Arduino微控制器上运行.我们可以在Ardunio IDE和Python IDLE中查看串口的输出.
我们接下来要做的是,在检测到大约30秒的运动后,发出电子邮件警报,看到我们此时没有以太网功能,我们认为最简单的方法是通过Python获取电子邮件.
怎么做到这一点?
此时我们可以发送来自Python的电子邮件,我们可以在Python中阅读Arduino串口,但是我们只是把它放在一起就有问题了.
这就是我们的Python代码看起来的样子,而1:是混乱发生的地方:
import smtplib,serial
ser = serial.Serial(port=2, baudrate=9200)
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
gmail_user = "usr@gmail.com"
gmail_pwd = "pw"
def mail(to, subject, text, attach):
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, …Run Code Online (Sandbox Code Playgroud) 嘿我正在为学生的细节做一个商店,我想要一些关于使用哪个系列的意见.商店将提供姓名,号码,地址和电子邮件等详细信息.然后将商店打印到文本文件,我可以在其中加载,保存,编辑和删除文本文件中的详细信息.我以前从未这样做过,如果在使用集合时对文件I/O有任何限制,我也不会这样做.所以我非常感谢这些评论.提前致谢.
.net ×3
threadpool ×2
algorithm ×1
arduino ×1
asynchronous ×1
c ×1
c# ×1
checkbox ×1
circuit ×1
collections ×1
concurrency ×1
datetime ×1
django ×1
django-forms ×1
email ×1
file-io ×1
fpga ×1
ios ×1
java ×1
many-to-many ×1
modelform ×1
objective-c ×1
precision ×1
python ×1
uibutton ×1