Inflation took an unexpected leap in August as sharply higher energy prices and housing costs sent the consumer price index up 0.5 percent, the Bureau of Labor Statistics reported yesterday.

It was the largest increase in the price index since January's 0.7 percent rise. Over the last eight months, consumer prices have gone up 5.1 percent on an annual basis. In 1986, the index rose 1.1 percent.

Analysts said it was unlikely that prices would spiral quickly upward during the rest of the year, but cautioned that the weak inflation rates of 1986 are gone. Prices in 1987 and 1988 are forecast to go up by 4 percent to 4.5 percent.

In another disappointing report, the Commerce Department said yesterday that new factory orders for durable goods, a gauge of future business investment, fell 3.1 percent last month. The decline was the first since January.

Orders for nondefense capital goods, a component of the durable-goods indicator that also predicts business spending, fell 8.4 percent in August from the previous month.

The stock market responded to the two reports with more optimism than might have been expected. The Dow Jones industrial average climbed 17.62 points to close at 2585.67.

"We are back on the track of 4 to 4.5 percent annual inflation," said Jerry Pegden, vice president and economist for the investment firm of Salomon Brothers Inc. in New York. "Inflation is not accelerating, but this report says the best news on inflation was in 1986. Deflation is behind us."

White House spokesman Marlin Fitzwater, calling the August price increase figures "temporary," said "recent data on consumer and producer prices indicate that inflation is running at the 4 percent pace reported during the first three years of the expansion."

Analysts generally had predicted a smaller increase, in the range of 0.2 percent to 0.3 percent, for the August CPI. The index went up 0.2 percent in July.

The largest increases in the CPI figures, which are adjusted for seasonal factors, came from oil prices, housing costs and educational expenses.

The BLS said gasoline prices rose a stiff 3.1 percent in August and have gone up 21.4 percent since January. All energy prices went up 1.7 percent last month.

However, oil prices have risen very little since those figures were gathered and little further pressure on prices is expected from the energy sector.

Food and beverage costs rose a slight 0.1 percent in August. The prices of food in grocery stores fell 0.1 percent, but the cost of beverages and meals in restaurants each went up 0.3 percent.

"I think most of the price increases that have come from energy, food and commodity prices are behind us," said John Oliver Wilson, chief economist for Bank of America in San Francisco. "In the future, inflation will be driven up by import prices, which have been going up very slowly because foreign producers have been trying to keep market share" as the falling dollar has made their goods more expensive in the United States.

Housing costs rose 0.6 percent last month. The housing component accounts for 27 percent of the entire CPI, and economists said the way those costs are measured, by estimating how much various types of houses would rent for, can produce wide fluctuations.

Nearly all of the 0.7 percent increase in "other goods and services" was due to increases in educational costs, the BLS said.

The August decline in durable goods orders to $105.8 billion, which economists generally had not expected, followed healthy increases in those orders during the second quarter of the year and, before that, a sharp decline in the first quarter of 1987. The fluctuations left analysts a bit puzzled about which way business capital spending was heading.

"It {the August fall} was a dramatic decline and I think it puts the whole capital goods picture up in the air," said Jerry Jasinowksi, chief economist of the National Association of Manufacturers. He suggested that companies may be cutting back because the tax revision law had reduced investment tax advantages, but added: "The swings in capital spending are very puzzling and don't show a clear trend."