首页 \ 问答 \ 为什么我没有获得浮点数?(Why am I not getting floating point numbers?)

为什么我没有获得浮点数?(Why am I not getting floating point numbers?)

我正在编写一个非常简单的练习脚本(我是新手),它将采用a,b,c和x的值并将它们插入到二次函数中。

出于某种原因,我没有在我的公式中获得浮点数。 我曾尝试将float()添加到比我感觉需要更多的地方,但我仍然获得了定点数字。

注意:在尝试几种方法后,我最终得到了很多浮点数输入; 这仅仅是最后一次迭代。 这是我的代码:

def quadratic(a, b, c, x):

    return float(a*x**2 + b*x + c)

print "Let's find the output value for the values in a quatratic function..."
print "Our formula is ax^2 + bx + c = ?"

a = float(raw_input("What value for 'a'?"))

b = float(raw_input("What value for 'b'?"))

c = float(raw_input("What value for 'c'?"))

x = float(raw_input("What value for 'x'?"))

solution = float(quadratic(a, b, c, x))

print "(%d)(%d)^2 + (%d)(%d) + %d = %d" % (a, x, b, x, c, solution)

这里是我的输出(在为a,b,c,x输入十进制值之后)。

Let's find the output value for the values in a quatratic function...

Our formula is ax^2 + bx + c = ?

What value for 'a'?.75

What value for 'b'?.44

What value for 'c'?.3467

What value for 'x'?.77

(0)(0)^2 + (0)(0) + 0 = 1

很明显,这是我的用户错误。 有人可以提供有关我为什么获得定点数的见解吗? 谢谢!


I am writing a very simple practice script (I am a novice) that will take in values of a, b, c, and x and plug them in to a quadratic function.

For some reason I am not getting floating point numbers into my equation. I have tried adding float() to more places than I feel I need to, and yet I'm still getting fixed point numbers.

Note: I ended up with this many float inputs after trying several ways; this is merely the last iteration. Here is my code:

def quadratic(a, b, c, x):

    return float(a*x**2 + b*x + c)

print "Let's find the output value for the values in a quatratic function..."
print "Our formula is ax^2 + bx + c = ?"

a = float(raw_input("What value for 'a'?"))

b = float(raw_input("What value for 'b'?"))

c = float(raw_input("What value for 'c'?"))

x = float(raw_input("What value for 'x'?"))

solution = float(quadratic(a, b, c, x))

print "(%d)(%d)^2 + (%d)(%d) + %d = %d" % (a, x, b, x, c, solution)

and here is my output (after entering decimal values for a, b, c, x).

Let's find the output value for the values in a quatratic function...

Our formula is ax^2 + bx + c = ?

What value for 'a'?.75

What value for 'b'?.44

What value for 'c'?.3467

What value for 'x'?.77

(0)(0)^2 + (0)(0) + 0 = 1

Clearly this is user error on my part. Can someone provide insight as to why I'm getting fixed point numbers? Thanks!


原文:https://stackoverflow.com/questions/45383632
更新时间:2022-12-13 12:12

最满意答案

这应该工作

awk '{ print $1+$2; }' file.txt

This should work

awk '{ print $1+$2; }' file.txt

相关问答

更多
  • 这应该工作 awk '{ print $1+$2; }' file.txt This should work awk '{ print $1+$2; }' file.txt
  • 有任何awk: $ awk 'BEGIN{FS=OFS="\t"} {for (i=2;i<=NF;i++) if ($i~/:FAIL$/) sub(/:[^:]+/,":-1",$i)} 1' file GT:CN:CNL:CNP:CNQ:FT .:2:a:b:c:PASS .:2:c:b:a:PASS .:-1:d:c:a:FAIL With any awk: $ awk 'BEGIN{FS=OFS="\t"} {for (i=2;i<=NF;i++) if ($i~/:FAIL$/) s ...
  • 如果你不能使用子进程模块,最好的办法是用Python重新编写你的AWK脚本。 为此,文件输入模块是一个很棒的过渡工具,它具有类似AWK的感觉。 If you can't use the subprocess module, the best bet is to recode your AWK script in Python. To that end, the fileinput module is a great transition tool with and AWK-like feel.
  • 你可以使用支持任意精度算术的bc 。 相当于你想要实现的目标是: cut -d' ' -f1 inputfile | paste -sd+ | bc -l 编辑:根据您的评论,如果您想防止将输出分成多行,请将BC_LINE_LENGTH设置为0 。 说: cut -d' ' -f1 inputfile | paste -sd+ | BC_LINE_LENGTH=0 bc -l You could use bc which supports arbitrary precision arithmetic. ...
  • 您绝对不能在单引号分隔的脚本中使用单引号,因为引用绝对表示该脚本[segment]的结尾。 您拥有的选项是: 将脚本放在一个文件“foo”中作为{print "'"$0}并且执行如下命令: awk -f foo file ,或者 在任何想要打印报价的地方使用转义序列\047 : awk '{print "\047"$0}' file ,或 将报价保存在变量中: awk -vq=\' '{print q$0}' file或 使用double而不是单引号分隔awk脚本并转义shell可能关心的任何内容:awk“ ...
  • 如果您的文件看起来像这样: col1 col2 col3 col1 col2 col3 用它来追加交替的行并删除回车: sed 'N;s/\r\n/ /' 结果如下: col1 col2 col3 col1 col2 col3 If your file looks something like this: col1 col2 col3 col1 col2 col3 Use this to append alternating lines and remove the carriage return: ...
  • 在Gnu Awk中,您可以使用\<和\>来匹配单词的开头和结尾,所以 gawk '/\/{++i} END{print i}' 会做同样的事情 grep -wc 'GOOD' file 如果你想计算GOOD这个词的出现总数(不仅是行数,还有给定行/记录中的出现次数),你可以在Gnu Awk版本4中使用FPAT , gawk 'BEGIN { FPAT="\\"; RS="^$" } { print NF }' file 如果要计算给定记录中短语GOOD DI的完全匹配数, ...
  • 您可以使用以下awk : awk -F, -v OFS=, 'NR==FNR{a[$1]=$2;next}{$2+=a[$1]}1' file1.csv file2.csv a,24325 b,433 我们将输入和输出字段分隔符设置为, 。 使用NR==FNR构造,我们将file1.csv加载到数组a 。 next允许我们使用第一个文件。 将文件加载到内存后,我们将移动到第二个文件,只需将数组中的值添加到第二列。 这会将输出打印到STDOUT。 您可以将输出重定向到另一个文件。 You can use t ...
  • 如果查询的结构保持不变,您可以使用: awk -F'[).]' '{print $3".%"}' 我使用右括号或文字点作为分隔符。 这样做的兴趣值在第3栏。 虽然它很简单,但会在用户面前留下一些空白。 我们可以增强字段分隔符regex来解决这个问题: awk -F')[[:space:]]*|[.]' '{print $3".%"}' 顺便说一句,你可以使用这个sed命令: sed 's/.*)[[:space:]]*\([^.]*\).*/\1.%/' 或者如果你有GNU grep,请使用: gre ...
  • print命令将以新行结束输出,因此您将每个数据项目放在新行上的原因。 如果用printf替换print ,那么每个输出将保留在“当前”输出行(即,不会在输出结尾处放置新行字符)。 请记住,您需要明确指定字段分隔符(例如空格,制表符),例如: printf "%s\t",$4 # will print field $4 followed by a tab 然后,当您想要终止一行时,您可以运行print ""或printf "\n"将新的行字符添加到输出结尾。 假设你想要一个标签作为字段分隔符,你可以将你的 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)