Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

makefold.expresion.cpp work with negative values#268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Delta456 wants to merge1 commit intochangkun:master
base:master
Choose a base branch
Loading
fromDelta456:patch-1

Conversation

Delta456
Copy link
Contributor

@Delta456Delta456 commentedJul 16, 2023
edited
Loading

resolve#267

Description

Please describe the motivation of this pull request, and what problem was solved in this PR.

Change List

Reference

I added astatic_cast<double> to make it work with negative values.


说明

此处详细说明 PR 的动机是什么、解决了什么样的问题。

变化箱单

  • 修复了 XXX 的 typo 错误
  • 增加了 XXX 相关的说明
  • 解决了关于 XXX 的描述性错误

参考文献

如果有请注明

@BaiLei27
Copy link

Do not use 'auto' for the return of template functions, but use 'double' instead

@@ -11,8 +11,8 @@
#include <iostream>
template<typename ... T>
auto average(T ... t) {
return(t + ... ) / sizeof...(t);
returnstatic_cast<double>((t + ...)) / sizeof...(t);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

我个人建议是模仿<cmath> 中浮点函数的额外重载,把整数类型转换为double 而浮点类型不变。这样可以避免long double 被转换为double

Suggested change
returnstatic_cast<double>((t + ... )) /sizeof...(t);
constexprauto integer_to_double = [](auto x) {
ifconstexpr (std::is_integral_v<decltype(x)>)
returnstatic_cast<double>(x);
else
return x;
};
return (integer_to_double(t) + ... ) /sizeof...(t);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

static_caste() it will explicitly change the data type of t to double

#include<iostream>template<typename ... T>autoaverage(T ... t) {return (static_cast<double>(t) + ...) /sizeof...(t);}intmain() {    std::cout <<average(1,2,3,4,5, -6, -7, -8, -9, -10) << std::endl;}}

@Devanshu-0808
Copy link

Devanshu-0808 commentedJun 8, 2024
edited
Loading

static_caste() it will explicitly change the data type of t to double

#include<iostream>template<typename ... T>autoaverage(T ... t) {return (static_cast<double>(t) + ...) /sizeof...(t);}intmain() {    std::cout <<average(1,2,3,4,5, -6, -7, -8, -9, -10) << std::endl;}

@@ -11,8 +11,8 @@
#include <iostream>
template<typename ... T>
auto average(T ... t) {
return(t + ... ) / sizeof...(t);
returnstatic_cast<double>((t + ...)) / sizeof...(t);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

static_caste() it will explicitly change the data type of t to double

#include<iostream>template<typename ... T>autoaverage(T ... t) {return (static_cast<double>(t) + ...) /sizeof...(t);}intmain() {    std::cout <<average(1,2,3,4,5, -6, -7, -8, -9, -10) << std::endl;}}

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@frederick-vs-jafrederick-vs-jafrederick-vs-ja left review comments

@Devanshu-0808Devanshu-0808Devanshu-0808 requested changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Chapter 2 exercise answer bug
4 participants
@Delta456@BaiLei27@Devanshu-0808@frederick-vs-ja

[8]ページ先頭

©2009-2025 Movatter.jp